CAShapeLayer的path动画

效果

源码

https://github.com/YouXianMing/Animations

//
// CAShapeLayerPathController.m
// Animations
//
// Created by YouXianMing on 15/11/17.
// Copyright © 2015年 YouXianMing. All rights reserved.
// #import "CAShapeLayerPathController.h"
#import "PathView.h"
#import "UIView+SetRect.h"
#import "GCD.h" @interface CAShapeLayerPathController () @property (nonatomic, strong) PathView *pathView;
@property (nonatomic, strong) GCDTimer *timer; @property (nonatomic, strong) CAShapeLayer *lineShapeLayer;
@property (nonatomic, strong) CAShapeLayer *fillShapeLayer; @property (nonatomic) CGPoint A; @property (nonatomic, strong) CALayer *pointA; @end @implementation CAShapeLayerPathController - (void)viewDidLoad { [super viewDidLoad];
} - (void)setup { [super setup]; // background view
self.pathView = [[PathView alloc] initWithFrame:CGRectMake(, , , )];
self.pathView.center = self.view.center;
self.pathView.gap = .f;
[self.pathView setNeedsDisplay];
[self.view addSubview:self.pathView];
UIBezierPath *path = [self randomPath]; // point A
self.pointA = [CALayer layer];
self.pointA.frame = CGRectMake(, , , );
self.pointA.cornerRadius = .f;
self.pointA.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f].CGColor;
self.pointA.position = CGPointMake(self.A.x + , self.A.y + );
[self.pathView.layer addSublayer:self.pointA]; // shape
self.fillShapeLayer = [CAShapeLayer layer];
self.fillShapeLayer.path = path.CGPath;
self.fillShapeLayer.strokeColor = [UIColor clearColor].CGColor;
self.fillShapeLayer.fillColor = [UIColor cyanColor].CGColor;
self.fillShapeLayer.lineWidth = .f;
self.fillShapeLayer.frame = CGRectMake(self.pathView.gap,
self.pathView.gap,
self.pathView.width - * self.pathView.gap,
self.pathView.width - * self.pathView.gap);
[self.pathView.layer addSublayer:self.fillShapeLayer]; // line
self.lineShapeLayer = [CAShapeLayer layer];
self.lineShapeLayer.path = path.CGPath;
self.lineShapeLayer.strokeColor = [UIColor redColor].CGColor;
self.lineShapeLayer.fillColor = [UIColor clearColor].CGColor;
self.lineShapeLayer.lineWidth = 0.5f;
self.lineShapeLayer.lineDashPattern = @[@(), @()];
self.lineShapeLayer.frame = CGRectMake(self.pathView.gap,
self.pathView.gap,
self.pathView.width - * self.pathView.gap,
self.pathView.width - * self.pathView.gap);
[self.pathView.layer addSublayer:self.lineShapeLayer]; // timer
self.timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
[self.timer event:^{ // path animation.
UIBezierPath *newPath = [self randomPath];
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
basicAnimation.duration = 0.5;
basicAnimation.fromValue = (__bridge id)(self.lineShapeLayer.path);
basicAnimation.toValue = (__bridge id)newPath.CGPath;
self.lineShapeLayer.path = newPath.CGPath;
self.fillShapeLayer.path = newPath.CGPath;
[self.lineShapeLayer addAnimation:basicAnimation forKey:@"lineShapeLayerPath"];
[self.fillShapeLayer addAnimation:basicAnimation forKey:@"fillShapeLayerPath"]; // fillColor animation.
UIColor *newColor = [self randomColor];
CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"fillColor"];
colorAnimation.duration = 0.5;
colorAnimation.fromValue = (__bridge id _Nullable)(self.fillShapeLayer.fillColor);
colorAnimation.toValue = (__bridge id)newColor.CGColor;
self.fillShapeLayer.fillColor = newColor.CGColor;
[self.fillShapeLayer addAnimation:colorAnimation forKey:@"fillShapeLayerColor"]; // path animation.
CGPoint newPoint = CGPointMake(self.A.x + , self.A.y + );
CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
positionAnimation.duration = 0.5f;
positionAnimation.fromValue = [NSValue valueWithCGPoint:self.pointA.position];
positionAnimation.toValue = [NSValue valueWithCGPoint:newPoint];
self.pointA.position = newPoint;
[self.pointA addAnimation:positionAnimation forKey:@"positionAnimation"]; } timeIntervalWithSecs:.f];
[self.timer start];
} - (UIBezierPath *)randomPath { CGPoint pointA = [self randomPointA];
CGPoint pointB = [self randomPointB];
CGPoint pointC = [self randomPointC];
CGPoint pointD = [self randomPointD]; self.A = pointA; UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:pointA];
[bezierPath addLineToPoint:pointB];
[bezierPath addLineToPoint:pointC];
[bezierPath addLineToPoint:pointD];
[bezierPath closePath]; return bezierPath;
} - (CGPoint)randomPointA { return CGPointMake(arc4random() % (int)(self.pathView.width - * self.pathView.gap), );
} - (CGPoint)randomPointB { return CGPointMake(self.pathView.width - * self.pathView.gap, arc4random() % (int)(self.pathView.width - * self.pathView.gap));
} - (CGPoint)randomPointC { return CGPointMake(arc4random() % (int)(self.pathView.width - * self.pathView.gap), self.pathView.width - * self.pathView.gap);
} - (CGPoint)randomPointD { return CGPointMake(, arc4random() % (int)(self.pathView.width - * self.pathView.gap));
} - (UIColor *)randomColor { return [UIColor colorWithRed:arc4random() % / .f
green:arc4random() % / .f
blue:arc4random() % / .f
alpha:0.5f];
} @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. C# 文件存在,但是File.Exists 判断不存在的问题

    这里说的不是文件路径错了的情况,而是明明文件就存在,但是File.Exists返回false. win10系统. 查看接口说明才知道,如果你不是按管理员方式启动VS,而此文件需要管理员权限才能访问,此 ...

  2. Java开发工程师学习路线

    贴一个比较出名的Java开发工程师学习路线图 好好学习提升中 这个貌似也不是特别全,算法,设计模式,架构好像都没有

  3. jquery中获取radio选中值的正确写法

    错误写法: //只在IE下有作用,其他浏览器均获取的为第一个单选框的值 $('input[type=radio]').val(); 正确写法为: //兼容所有浏览器写法 $('input[type=r ...

  4. 简单实现textview文本每隔两秒就改变一次

    //这个方法可以实现文本每隔两秒就改变一次, public void textTask(){ final android.os.Handler handler=new android.os.Handl ...

  5. 【知了堂学习笔记】java 接口与抽象类

    本次主角:抽象类 .接口. 对于皮皮潇这样一类的Java初学者来说,接口和抽象类如果不去花大量的精力与时间是很难弄清楚的,而我也是在最近这周的项目学习中感觉到了我对这两个概念不熟悉,所以导致对一些问题 ...

  6. SQL注入使用Django中继数据包bypassWAF

    原理 本人基于文章bypassword的文章在HTTP协议层面绕过WAF所编写一款工具. 环境 Python3.7.0 Django 2.1 Requests 使用范围 POST注入 可以分块传输的漏 ...

  7. 【基本功】深入剖析Swift性能优化

    简介 2014年,苹果公司在WWDC上发布Swift这一新的编程语言.经过几年的发展,Swift已经成为iOS开发语言的“中流砥柱”,Swift提供了非常灵活的高级别特性,例如协议.闭包.泛型等,并且 ...

  8. 【AI in 美团】深度学习在文本领域的应用

    背景 近几年以深度学习技术为核心的人工智能得到广泛的关注,无论是学术界还是工业界,它们都把深度学习作为研究应用的焦点.而深度学习技术突飞猛进的发展离不开海量数据的积累.计算能力的提升和算法模型的改进. ...

  9. JAVAEE——ssm综合练习:CRM系统(包含ssm整合)

    1 CRM项目外观   1. 开发环境 IDE: Eclipse Mars2 Jdk: 1.7 数据库: MySQL 2. 创建数据库 数据库sql文件位置如下图: 创建crm数据库,执行sql 效果 ...

  10. 1024 Palindromic Number (25)(25 point(s))

    problem A number that will be the same when it is written forwards or backwards is known as a Palind ...