iOS渐变圆环旋转动画CAShapeLayer CAGradientLayer

 更新时间:2016年09月20日 11:02:08   作者:Eric__li  
这篇文章主要介绍了iOS渐变圆环旋转动画CAShapeLayer CAGradientLayer的相关资料,需要的朋友可以参考下

iOS渐变圆环旋转动画CAShapeLayer CAGradientLayer

shape.gif

demo.png

- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.

 CALayer *layer = [CALayer layer];
 layer.backgroundColor = [UIColor redColor].CGColor; //圆环底色
 layer.frame = CGRectMake(100, 100, 110, 110);


 //创建一个圆环
 UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(55, 55) radius:50 startAngle:0 endAngle:M_PI*2 clockwise:YES];

 //圆环遮罩
 CAShapeLayer *shapeLayer = [CAShapeLayer layer];
 shapeLayer.fillColor = [UIColor clearColor].CGColor;
 shapeLayer.strokeColor = [UIColor redColor].CGColor;
 shapeLayer.lineWidth = 5;
 shapeLayer.strokeStart = 0;
 shapeLayer.strokeEnd = 0.8;
 shapeLayer.lineCap = @"round";
 shapeLayer.lineDashPhase = 0.8;
 shapeLayer.path = bezierPath.CGPath;

 //颜色渐变
 NSMutableArray *colors = [NSMutableArray arrayWithObjects:(id)[UIColor redColor].CGColor,(id)[UIColor whiteColor].CGColor, nil];
 CAGradientLayer *gradientLayer = [CAGradientLayer layer];
 gradientLayer.shadowPath = bezierPath.CGPath;
 gradientLayer.frame = CGRectMake(50, 50, 60, 60);
 gradientLayer.startPoint = CGPointMake(0, 1);
 gradientLayer.endPoint = CGPointMake(1, 0);
 [gradientLayer setColors:[NSArray arrayWithArray:colors]];
 [layer addSublayer:gradientLayer]; //设置颜色渐变
 [layer setMask:shapeLayer]; //设置圆环遮罩
 [self.view.layer addSublayer:layer];

 //动画
 CABasicAnimation *scaleAnimation1 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
 scaleAnimation1.fromValue = [NSNumber numberWithFloat:1.0];
 scaleAnimation1.toValue = [NSNumber numberWithFloat:1.5];
 scaleAnimation1.autoreverses = YES;
// scaleAnimation1.fillMode = kCAFillModeForwards;
 scaleAnimation1.duration = 0.8;

 CABasicAnimation *rotationAnimation2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
 rotationAnimation2.fromValue = [NSNumber numberWithFloat:0];
 rotationAnimation2.toValue = [NSNumber numberWithFloat:6.0*M_PI];
 rotationAnimation2.autoreverses = YES;
// scaleAnimation.fillMode = kCAFillModeForwards;
 rotationAnimation2.repeatCount = MAXFLOAT;
 rotationAnimation2.beginTime = 0.8; //延时执行,注释掉动画会同时进行
 rotationAnimation2.duration = 2;


 //组合动画
 CAAnimationGroup *groupAnnimation = [CAAnimationGroup animation];
 groupAnnimation.duration = 4;
 groupAnnimation.autoreverses = YES;
 groupAnnimation.animations = @[scaleAnimation1, rotationAnimation2];
 groupAnnimation.repeatCount = MAXFLOAT;
 [layer addAnimation:groupAnnimation forKey:@"groupAnnimation"];

}

- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}
@end

关键的地方在于CABasicAnimation对象的初始化方式中keyPath的设定。在iOS中有以下几种不同的keyPath,代表着不同的效果:

以上就是iOS渐变圆环旋转动画 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

相关文章

  • IOS之UIWebView的使用(基本知识)

    IOS之UIWebView的使用(基本知识)

    在Android开发中有WebView作为混合模式开发的桥梁,当然在IOS中也同样有一个 UIWebView 组件来作为混合模式开发的桥梁,那么下面就对UIWebView的一些基本知识详解一下
    2016-02-02
  • IOS设计模式之组合设计模式

    IOS设计模式之组合设计模式

    组合模式,Composite Pattern,是一个非常巧妙的模式。几乎所有的面向对象系统都应用到了组合模式,接下来通过本文给大家介绍IOS设计模式之组合设计模式,需要的朋友参考下
    2016-02-02
  • iOS中sqlite的详细用法

    iOS中sqlite的详细用法

    在iOS中,也同样支持sqlite。目前有很多第三方库,封装了sqlite操作,比如swift语言写的SQLite.swift,对sqlite感兴趣的小伙伴们可以参考一下
    2016-05-05
  • iOS10添加本地推送(Local Notification)实例

    iOS10添加本地推送(Local Notification)实例

    这篇文章主要为大家详细介绍了iOS10添加本地推送(Local Notification)实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • 一道值得深入思考的iOS面试题详解

    一道值得深入思考的iOS面试题详解

    这篇文章主要给大家分享介绍了关于一道值得深入思考的iOS面试题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-02-02
  • iOS项目的开发命名规范教程

    iOS项目的开发命名规范教程

    为了团队各成员之间代码的互通、可读、易维护性,特制订此开发规范。下面这篇文章主要给大家介绍了关于iOS项目的开发命名规范的相关资料,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2017-11-11
  • 模仿iOS版微信的滑动View效果

    模仿iOS版微信的滑动View效果

    比如我们常用的微信,对于Android版本,长按某个聊天好友,会弹出 标为未读,置顶聊天,删除聊天 选项;对于iOS的版本,右滑,会显示出 标为未读,删除 选项。这篇文章主要介绍了模仿iOS版微信的滑动View,需要的朋友可以参考下
    2019-05-05
  • iOS开发使用XML解析网络数据

    iOS开发使用XML解析网络数据

    XML解析其实这个概念出现了算够久了,以前javaweb什么到处都在用。这边我们主要大致介绍下,然后在在ios编程如何使用。
    2016-02-02
  • iOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自己对未知的好奇心,还经常能发现一些意外的惊喜。这篇文章主要介绍了iOS通过逆向如何深入理解Block内存模型的相关资料。
    2017-01-01
  • Objective-C实现无限循环轮播器

    Objective-C实现无限循环轮播器

    这篇文章主要介绍了Objective-C实现无限循环轮播器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05

最新评论