首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >绘图- 镂空效果及其动画实现解析

绘图- 镂空效果及其动画实现解析

作者头像
進无尽
发布2018-09-12 18:05:22
2K0
发布2018-09-12 18:05:22
举报
文章被收录于专栏:進无尽的文章進无尽的文章

前言

有时你会看到很多镂空的试图或者是镂空视图的动画效果,感觉很酷炫,其实只要掌握其中实现的原理,想实现怎样的效果就能实现怎样的镂空效果。

原理解析

  • UIView的maskView属性。
  • CALayer的mask属性(也是一个CALayer)。 通过控制UIView的maskView、CALayer的mask有效区域,都可以修改UIView和 UIView的layer的显示外形,从而得到镂空或者其他的奇特形状及其动画。

实现关键点

图层中,因为UIView的layer为CALayer,不像CAShapeLayer那样有Path属性,所以我们无法直接修改layer的的显示形状,唯一能是layer显示出奇特形状的方法只有两种。

  • 使用图片作为mask可以直接获得需要显示的外形,需要注意的是这样的图片中需要展示的区域必须有像素,不需要显示的地方不可以有像素为空白,才能出效果,而且有像素的区域的透明度也会影响到最终的效果。
  • 使用自定义形状的CAShapeLayer作为mask也可以达到使图层显示出镂空的效果。同样的,图层显示出来的区域是 CAShapeLayer的外形。 (1) 这里特别强调下,当CAShapeLayer没有设置backgroundColor时,(默认为backgroundColor = UIColor clearColor.CGColor),CAShapeLayer的显示区域就是它的 path区域(一般为UIBezierPath绘制。),这种情况下CAShapeLayer的fillColor的透明度,也会对图层的显示起到影响作用,图层最终的显示只跟fillColor的透明度有关,跟fillColor的颜色无关。 (2) 当CAShapeLayer的backgroundColor不是clearColor的时候,CAShapeLayer的显示区域就是它本身的Frame,跟它的path区域就没关系了。 (3) CAShapeLayer的path区域不能超出CAShapeLayer的Frame,超出部分不会对图层的显示起作用。

例子

叶子状进度条

叶子状进度条.gif

self.waveView = [[JWWavesAnimationView alloc] initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width - 200) / 2, ([[UIScreen mainScreen] bounds].size.height - 120) / 2, 200, 120)];
[_waveView setUp];
[self.view addSubview:self.waveView];

//mask 蒙版
CALayer *maskLayer = [CALayer layer];
[maskLayer setFrame:self.waveView.bounds];
maskLayer.contents = (id)[UIImage imageNamed:@"123456"].CGImage;

self.waveView.layer.mask = maskLayer;

使用了图片作为遮罩图层,self.waveView为一个水波上涨的自定义试图,其中水波的上升效果是通过核心动画和 CAShapeLayer的path动态绘制实现的,先了解更多的可以看我的其他两篇文章:

绘图-视图遮罩MaskView的使用

绘图-类似百度外卖波浪效果的实现与关键点解析

叶子状裁图
UIImageView *imageV  =[[UIImageView alloc] initWithFrame:CGRectMake(0,100,300,150)];
imageV.image = [UIImage  imageNamed:@"joey-kyber-132520.jpg"];
imageV.backgroundColor = [UIColor clearColor];
[self.view addSubview:imageV];

//mask 蒙版
CALayer *maskLayer = [CALayer layer];
[maskLayer setFrame:imageV.bounds];
maskLayer.contents = (id)[UIImage imageNamed:@"123456"].CGImage;

imageV.layer.mask = maskLayer;
一个镂空进度图的分布解析

背景red.gif

self.waveSinLayer = [CAShapeLayer layer];
_waveSinLayer.backgroundColor = [UIColor redColor].CGColor;
self.waveSinLayer.fillColor = [[UIColor greenColor] CGColor];
self.waveSinLayer.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height);
[self.layer addSublayer:self.waveSinLayer];

self.waveCosLayer = [CAShapeLayer layer];
_waveCosLayer.backgroundColor = [UIColor clearColor].CGColor;
self.waveCosLayer.fillColor = [[UIColor blueColor] CGColor];
self.waveCosLayer.frame = CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height);
[self.layer addSublayer:self.waveCosLayer];

因为我设置了self.waveSinLayer的背景为红色,所以它里面的path形成的波浪便不再显示,而 self.waveCosLayer的背景设置为clearColor就可以看见它的蓝色波浪了。

背景clear.gif

设置了self.waveSinLayer的背景和self.waveCosLayer的背景都为clearColor。

移动.gif

CGPoint position = self.waveSinLayer.position;
position.y = position.y - self.bounds.size.height-10;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:self.waveSinLayer.position];
animation.toValue = [NSValue valueWithCGPoint:position];
animation.duration = kWavePositionDuration;
animation.repeatCount = HUGE_VALF;
animation.removedOnCompletion = NO;

[self.waveSinLayer addAnimation:animation forKey:@"positionWave"];
[self.waveCosLayer addAnimation:animation forKey:@"positionWave"];

使用CABasicAnimation设置动画使波浪图层上移。

最终效果.gif

_grayImageView = [[UIImageView alloc] initWithFrame:self.bounds];
_grayImageView.image = [UIImage imageNamed:@"du.png"];
 [self addSubview:_grayImageView];

_cosineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, self.frame.size.width, self.frame.size.height)];
_cosineImageView.image = [UIImage imageNamed:@"gray.png"];

_sineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, self.frame.size.width, self.frame.size.height)];
_sineImageView.image = [UIImage imageNamed:@"blue.png"];

_waveSinLayer.mask = _sineImageView.layer;
_waveCosLayer.mask = _cosineImageView.layer;

设置mask即可实现其中两张图片的动态波浪展示。

其中的三张图片为:

du.png

gray.png

blue.png


小结

有问题可以留言交流哦。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 原理解析
  • 实现关键点
  • 例子
    • 叶子状进度条
      • 叶子状裁图
        • 一个镂空进度图的分布解析
        • 小结
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档