iOS动画之CAShapeLayer(二)爱奇艺加载动画

QIY.gif
从今天起demo的代码分OC和Swift两种,以便满足所有同学的需求,但是讲解代码还是OC

突然发现爱奇艺的视频加载动画可以用CAShapeLayer做出来,之前在提交动画里已经详细介绍过CAShapeLayer用法及其属性,不懂得请看iOS动画之CAShapeLayer(一)提交动画

任何动画都是一步步完成的,所以以后遇到复杂的动画不要着急,把动画拆分了,就不难了。

拆分动画

  1. 一个圆慢慢画出来;
  2. 圆慢慢的消失;
  3. 圆消失的同时三角形旋转360度。

一个圆慢慢画出来

还记得上个文章里的话吗:理论上,所有描线的动画你都可以用这种方式先指定一个 path 然后改变 strokeEnd, strokeStart 来实现。

那么一个圆慢慢出来怎么做,首先需要一个path,path其实是个圆

//画一个圆
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.bounds];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.fillColor=[UIColor clearColor].CGColor;
//将路径赋值给CAShapeLayer
maskLayer.path = path.CGPath;
//设置路径的颜色
maskLayer.strokeColor=[UIColor colorWithRed:0.52f green:0.76f blue:0.07f alpha:1.00f].CGColor;
//设置路径的宽度
maskLayer.lineWidth=1;
maskLayer.lineCap=kCALineCapRound;

[self.layer addSublayer:maskLayer];

接下来要开始动画了

self.maskLayer.strokeStart=0;
//设置strokeEnd的最终值,动画的fromValue为0,strokeEnd的最终值为0.98
self.maskLayer.strokeEnd=0.98;

CABasicAnimation *BasicAnimation=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
BasicAnimation.fromValue=@(0);
BasicAnimation.duration=NSTimeInterval;
BasicAnimation.delegate=self;
[BasicAnimation setValue:@"BasicAnimationEnd" forKey:@"animationName"];
[self.maskLayer addAnimation:BasicAnimation forKey:@"BasicAnimationEnd"];

这样完成后一个圆就慢慢出现

setup1.gif

圆慢慢的消失

怎么让一个圆慢慢消失呢,那请问你怎么让一个圆出的?是通过改变strokeEnd从0->0.98
画圆结束后strokeEnd为0.98
那么让strokeStart从0->0.98 strokeStart与strokeEnd一样那么圆不就消失了吗

self.maskLayer.strokeStart=0.98;
CABasicAnimation *BasicAnimation=[CABasicAnimation animationWithKeyPath:@"strokeStart"];
BasicAnimation.fromValue=@(0);
BasicAnimation.duration=NSTimeInterval;
BasicAnimation.delegate=self;
[BasicAnimation setValue:@"BasicAnimationStart" forKey:@"animationName"];
[self.maskLayer addAnimation:BasicAnimation forKey:@"BasicAnimationStart"];
setup2.gif

代码和画圆差不多,就不多解释了

圆消失的同时三角形旋转360度

//开始三角形旋转

    CABasicAnimation *BasicAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    BasicAnimation.toValue=@(M_PI*2);
    BasicAnimation.duration=NSTimeInterval;
    BasicAnimation.delegate=self;
    [BasicAnimation setValue:@"BasicAnimationRotation" forKey:@"animationName"];
    [self.centerImage.layer addAnimation:BasicAnimation forKey:@"BasicAnimationRotation"];

最基本的CABasicAnimation真的不需要多解释了
如果感觉这篇文章对您有所帮助,顺手点个喜欢,谢谢啦
代码放在了GitHub上大家可以下载。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • CAShapeLayer与UIBezierPath画出想要的图形 CAShapeLayer和drawRect的比较...
    leonardni阅读 5,765评论 0 0
  • 现在在苹果应用商店上有超过140万的App,想让你的app事件非常具有挑战的事情。你有这样一个机会,在你的应用的数...
    zmp1123阅读 11,484评论 0 33
  • 蜀葵在贫瘠中扎根,继而膨胀就像汗水先是在额头沁出然后似水蛇般向心窝蠕动、奔跑太阳的溺爱让人近乎缺氧在六月 干涸的喉...
    墨成阅读 1,720评论 1 4
  • 乍暖还寒的初春,我踏上了独自一人的回乡旅程。为了父亲,也为了自己。 周六的一个早晨,我象往常一样打电话给妈妈寒喧,...
    绽放美丽阅读 1,747评论 6 8
  • 又要感慨一番了,这次是真正的感受到了语言的力量。说话真的是一把双刃剑啊,话说好了能给人带来无限的好处,然而有更多...
    老友miil阅读 1,688评论 0 0

友情链接更多精彩内容