CAShapeLayer的path动画

效果

源码

https://github.com/YouXianMing/Animations

  1. //
  2. // CAShapeLayerPathController.m
  3. // Animations
  4. //
  5. // Created by YouXianMing on 15/11/17.
  6. // Copyright © 2015年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "CAShapeLayerPathController.h"
  10. #import "PathView.h"
  11. #import "UIView+SetRect.h"
  12. #import "GCD.h"
  13.  
  14. @interface CAShapeLayerPathController ()
  15.  
  16. @property (nonatomic, strong) PathView *pathView;
  17. @property (nonatomic, strong) GCDTimer *timer;
  18.  
  19. @property (nonatomic, strong) CAShapeLayer *lineShapeLayer;
  20. @property (nonatomic, strong) CAShapeLayer *fillShapeLayer;
  21.  
  22. @property (nonatomic) CGPoint A;
  23.  
  24. @property (nonatomic, strong) CALayer *pointA;
  25.  
  26. @end
  27.  
  28. @implementation CAShapeLayerPathController
  29.  
  30. - (void)viewDidLoad {
  31.  
  32. [super viewDidLoad];
  33. }
  34.  
  35. - (void)setup {
  36.  
  37. [super setup];
  38.  
  39. // background view
  40. self.pathView = [[PathView alloc] initWithFrame:CGRectMake(, , , )];
  41. self.pathView.center = self.view.center;
  42. self.pathView.gap = .f;
  43. [self.pathView setNeedsDisplay];
  44. [self.view addSubview:self.pathView];
  45. UIBezierPath *path = [self randomPath];
  46.  
  47. // point A
  48. self.pointA = [CALayer layer];
  49. self.pointA.frame = CGRectMake(, , , );
  50. self.pointA.cornerRadius = .f;
  51. self.pointA.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f].CGColor;
  52. self.pointA.position = CGPointMake(self.A.x + , self.A.y + );
  53. [self.pathView.layer addSublayer:self.pointA];
  54.  
  55. // shape
  56. self.fillShapeLayer = [CAShapeLayer layer];
  57. self.fillShapeLayer.path = path.CGPath;
  58. self.fillShapeLayer.strokeColor = [UIColor clearColor].CGColor;
  59. self.fillShapeLayer.fillColor = [UIColor cyanColor].CGColor;
  60. self.fillShapeLayer.lineWidth = .f;
  61. self.fillShapeLayer.frame = CGRectMake(self.pathView.gap,
  62. self.pathView.gap,
  63. self.pathView.width - * self.pathView.gap,
  64. self.pathView.width - * self.pathView.gap);
  65. [self.pathView.layer addSublayer:self.fillShapeLayer];
  66.  
  67. // line
  68. self.lineShapeLayer = [CAShapeLayer layer];
  69. self.lineShapeLayer.path = path.CGPath;
  70. self.lineShapeLayer.strokeColor = [UIColor redColor].CGColor;
  71. self.lineShapeLayer.fillColor = [UIColor clearColor].CGColor;
  72. self.lineShapeLayer.lineWidth = 0.5f;
  73. self.lineShapeLayer.lineDashPattern = @[@(), @()];
  74. self.lineShapeLayer.frame = CGRectMake(self.pathView.gap,
  75. self.pathView.gap,
  76. self.pathView.width - * self.pathView.gap,
  77. self.pathView.width - * self.pathView.gap);
  78. [self.pathView.layer addSublayer:self.lineShapeLayer];
  79.  
  80. // timer
  81. self.timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
  82. [self.timer event:^{
  83.  
  84. // path animation.
  85. UIBezierPath *newPath = [self randomPath];
  86. CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
  87. basicAnimation.duration = 0.5;
  88. basicAnimation.fromValue = (__bridge id)(self.lineShapeLayer.path);
  89. basicAnimation.toValue = (__bridge id)newPath.CGPath;
  90. self.lineShapeLayer.path = newPath.CGPath;
  91. self.fillShapeLayer.path = newPath.CGPath;
  92. [self.lineShapeLayer addAnimation:basicAnimation forKey:@"lineShapeLayerPath"];
  93. [self.fillShapeLayer addAnimation:basicAnimation forKey:@"fillShapeLayerPath"];
  94.  
  95. // fillColor animation.
  96. UIColor *newColor = [self randomColor];
  97. CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"fillColor"];
  98. colorAnimation.duration = 0.5;
  99. colorAnimation.fromValue = (__bridge id _Nullable)(self.fillShapeLayer.fillColor);
  100. colorAnimation.toValue = (__bridge id)newColor.CGColor;
  101. self.fillShapeLayer.fillColor = newColor.CGColor;
  102. [self.fillShapeLayer addAnimation:colorAnimation forKey:@"fillShapeLayerColor"];
  103.  
  104. // path animation.
  105. CGPoint newPoint = CGPointMake(self.A.x + , self.A.y + );
  106. CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
  107. positionAnimation.duration = 0.5f;
  108. positionAnimation.fromValue = [NSValue valueWithCGPoint:self.pointA.position];
  109. positionAnimation.toValue = [NSValue valueWithCGPoint:newPoint];
  110. self.pointA.position = newPoint;
  111. [self.pointA addAnimation:positionAnimation forKey:@"positionAnimation"];
  112.  
  113. } timeIntervalWithSecs:.f];
  114. [self.timer start];
  115. }
  116.  
  117. - (UIBezierPath *)randomPath {
  118.  
  119. CGPoint pointA = [self randomPointA];
  120. CGPoint pointB = [self randomPointB];
  121. CGPoint pointC = [self randomPointC];
  122. CGPoint pointD = [self randomPointD];
  123.  
  124. self.A = pointA;
  125.  
  126. UIBezierPath* bezierPath = [UIBezierPath bezierPath];
  127. [bezierPath moveToPoint:pointA];
  128. [bezierPath addLineToPoint:pointB];
  129. [bezierPath addLineToPoint:pointC];
  130. [bezierPath addLineToPoint:pointD];
  131. [bezierPath closePath];
  132.  
  133. return bezierPath;
  134. }
  135.  
  136. - (CGPoint)randomPointA {
  137.  
  138. return CGPointMake(arc4random() % (int)(self.pathView.width - * self.pathView.gap), );
  139. }
  140.  
  141. - (CGPoint)randomPointB {
  142.  
  143. return CGPointMake(self.pathView.width - * self.pathView.gap, arc4random() % (int)(self.pathView.width - * self.pathView.gap));
  144. }
  145.  
  146. - (CGPoint)randomPointC {
  147.  
  148. return CGPointMake(arc4random() % (int)(self.pathView.width - * self.pathView.gap), self.pathView.width - * self.pathView.gap);
  149. }
  150.  
  151. - (CGPoint)randomPointD {
  152.  
  153. return CGPointMake(, arc4random() % (int)(self.pathView.width - * self.pathView.gap));
  154. }
  155.  
  156. - (UIColor *)randomColor {
  157.  
  158. return [UIColor colorWithRed:arc4random() % / .f
  159. green:arc4random() % / .f
  160. blue:arc4random() % / .f
  161. alpha:0.5f];
  162. }
  163.  
  164. @end

细节

注意keyPath参数。

CAShapeLayer的path动画的更多相关文章

  1. 用path动画绘制水波纹

    用path动画绘制水波纹 效果 源码 // // ViewController.m // PathAnimation // // Created by YouXianMing on 15/7/3. / ...

  2. 使用CAShapeLayer的path属性与UIBezierPath画出扫描框

    1.CAShapeLayer CAShapeLayer具有path属性,(是CGPath对象),可以使用这个属性与UIBezierPath画出想要的图形.该子类根据其fill color和stroke ...

  3. iOS | CAShapeLayer转场动画

    什么也不说了,作为一名乐于分享技术的小开发,直接先上个样式最为直观贴切,有需要的朋友可以直接拿过去用. 需要demo请点击这里 :github 在这个demo中,核心为选用画布CAShapeLayer ...

  4. CAShapeLayer+CADisplayLink 波浪动画

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0. ...

  5. svg path 动画效果

    http://www.zhangxinxu.com/wordpress/2014/04/animateion-line-drawing-svg-path-%E5%8A%A8%E7%94%BB-%E8% ...

  6. iOS酷炫动画效果合集

    iOS酷炫动画效果合集 源码地址 https://github.com/YouXianMing/Animations 效果绝对酷炫,包含了多种多样的动画类型,如POP.Easing.粒子效果等等,虽然 ...

  7. 动画黄金搭档:CADisplayLink&CAShapeLayer

    我们在开发中有时会遇到一些看似非常复杂的动画,不知该如何下手,今天的这篇文章中我会讲到如何利用CADisplayLink和CAShapeLayer来构建一些复杂的动画,希望能在你下次构建动画中,给你一 ...

  8. 动画黄金搭档:CADisplayLink & CAShapeLayer

    我们在开发中有时会遇到一些看似非常复杂的动画,不知该如何下手,今天的这篇文章中我会讲到如何利用CADisplayLink和CAShapeLayer来构建一些复杂的动画,希望能在你下次构建动画中,给你一 ...

  9. iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画

    CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...

随机推荐

  1. PHP性能调优---PHP-FPM配置及使用总结

    PHP-FPM配置及使用总结: php-FPM是一个PHP FastCGI的管理器,它实际上就是PHP源代码的补丁,旨在将FastCGI进程管理引进到PHP软件包中,我们必须将其patch到PHP源代 ...

  2. 浏览器开启web通知。

    https://www.cnblogs.com/xcsn/p/7767092.html

  3. gluster学习(一)

    2)Bricks • Brick是一个节点和一个导出目录的集合,e.g. node1:/brick1 • Brick是底层的RAID或磁盘经XFS或ext4文件系统格式化而来,所以继承了文件系统的限制 ...

  4. 再议js的传递和深复制

    病理 基本类型的传递就是按值传递,比如说 var a = 1; var b = a; b = 3; console.log(a,b);//1,3 很明显,a的值并未因为b的值改变而变化,这是因为a只是 ...

  5. Ionic入门三:列表

    列表是一个应用广泛的界面元素,在所有移动app中几乎都会使用到. 列表可以是基本文字.按钮,开关,图标和缩略图等. 列表项可以是任何的HTML元素.容器元素需要list类,每个列表项需要使用item类 ...

  6. 第一个iOS程序:Hello iOS

    今天我们来创建第一个iOS程序:Hello iOS!不需要写任何代码就能实现:

  7. thinkphp 5.0 lnmp环境下 无法访问,报错500(public目录)

    两种方法: 1.修改fastcgi的配置文件 /usr/local/nginx/conf/fastcgi.conf fastcgi_param PHP_ADMIN_VALUE "open_b ...

  8. 异步任务 -- FutureTask

    任务提交 之前在分析线程池的时候,提到过 AbstractExecutorService 的实现: public Future<?> submit(Runnable task) { if ...

  9. awk 基本函数用法

    gsub函数有点类似于sed查找和替换.它允许替换一个字符串或字符为另一个字符串或字符,并以正则表达式的形式执行.第一个函数作用于记录$0,第二个gsub函数允许指定目标,然而,如果未指定目标,缺省为 ...

  10. 【Performance】chrome调试面板

    本篇文章以chrome版本67.0.3396.99为例,说明性能方面的调试.