在阅读本文之前,对CAShapeLayer、UIBezierPath不熟悉的话,可以先阅读文章 贝塞尔曲线与Layer

如果对动画不熟悉的话,先阅读文章 动画基础、深入

Layer是绘图的画板,Bezier是画图的画笔,Animation是画图的动作。现在我们可以通过下面例子更好的让它们更好地结合在一起。

1)画一个小屋子动画

我们首先通过定义CAShapeLayer画板,然后定义path来确定画图路径。最后使用动画。如下面代码

     //让一个屋子的线画起来的动画效果
func addCAShapeLayerAnimationStrokeEnd() {
//创建CAShapeLayer,屋子的layer
let slayer = CAShapeLayer.init()
slayer.strokeColor = UIColor.blackColor().CGColor
slayer.fillColor = UIColor.clearColor().CGColor //创建屋子的路径path
let path = UIBezierPath.init()
path.moveToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, )) //把画图的路径path添加到layer中
slayer.path = path.CGPath
//添加slayer到view.layer
self.view.layer.addSublayer(slayer) //创建动画,strokeEnd。让线画起来的效果
let ani = CABasicAnimation(keyPath: "strokeEnd")
ani.fromValue =
ani.toValue =
ani.repeatCount =
ani.duration = slayer.addAnimation(ani, forKey: "addCAShapeLayerAnimationStrokeEnd")
}

2)小屋子+画笔的动画

上面1)中我们画了一个屋子的动画,那么如果我们想在屋子上面添加一个画笔来画,让动画更加生动。所以我们用到keyframe动画来添加path。

     //让一个屋子的线画起来的动画效果
func addCAShapeLayerAnimationStrokeEnd2() {
//---创建CAShapeLayer,屋子的layer start---
let pathLayer = CAShapeLayer.init()
pathLayer.strokeColor = UIColor.blackColor().CGColor
pathLayer.fillColor = UIColor.clearColor().CGColor
//创建屋子的路径path
let path = UIBezierPath.init()
path.moveToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, ))
path.addLineToPoint(CGPointMake(, ))
//把画图的路径path添加到layer中
pathLayer.path = path.CGPath
//添加slayer到view.layer
self.view.layer.addSublayer(pathLayer)
//---创建CAShapeLayer,屋子的layer end--- //---创建画笔layer start---
let panLayer = CALayer.init()
panLayer.anchorPoint = CGPointZero
panLayer.frame = CGRectMake(, , , )
panLayer.contents = UIImage(named: "pan.png")?.CGImage
self.view.layer.addSublayer(panLayer)
//---创建画笔layer end--- //---创建动画,strokeEnd。让线画起来的效果 start---
let strokeEndAnimation = CABasicAnimation(keyPath: "strokeEnd")
strokeEndAnimation.fromValue =
strokeEndAnimation.toValue =
strokeEndAnimation.repeatCount =
strokeEndAnimation.duration =
pathLayer.addAnimation(strokeEndAnimation, forKey: "strokeEndAnimation")
//---创建动画,strokeEnd。让线画起来的效果 end--- //---让画笔动起来动画start---
let panAnimation = CAKeyframeAnimation(keyPath: "position")
panAnimation.path = path.CGPath
panAnimation.calculationMode = kCAAnimationPaced
panAnimation.duration =
panAnimation.repeatCount =
panLayer.addAnimation(panAnimation, forKey: "panAnimation")
//---让画笔动起来动画end--- }

IOS Animation-CAShapeLayer、UIBezierPath与Animation的结合的更多相关文章

  1. Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)

    1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...

  2. iOS 动画篇 之 Core Animation (一)

    iOS中实现动画有两种方式,一种是自己不断的通过drawRect:方法来绘制,另外一种就是使用核心动画(Core Animation). 导语: 核心动画提供高帧速率和流畅的动画,而不会增加CPU的负 ...

  3. iOS Swift最简单的Animation

    最近发现Animation是一个iOS开发中非常好玩的元素,能给应用的交互性增色不少.比如很多音乐应用的菜单从底部弹出和隐藏的效果. Animation最核心的当然就是UIView的animateWi ...

  4. iOS 动画效果:Core Animation & Facebook's pop

    本文转载至 http://www.cocoachina.com/ios/20151223/14739.html 感谢原创作者分享 前言相信很多人对实现 iOS 中的动画效果都特别头疼,往往懒得动手,功 ...

  5. IOS 核心动画(Core Animation)

    Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它 能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就 可以实现非常强大的功能. Core ...

  6. iOS 图形图像动画 Core Animation

    //Core Animation #define WeakSelf __weak __typeof(self) weakSelf = self #define StrongSelf __strong ...

  7. iOS关于CAShapeLayer与UIBezierPath的知识内容

    使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...

  8. IOS中的动画——Core Animation

    一.基础动画 CABasicAnimation //初始化方式 CABasicAnimation * cabase=[CABasicAnimation animation]; //通过keyPath设 ...

  9. CAShapeLayer + UIBezierPath

    UIBezierPath: UIBezierPath是在 UIKit 中的一个类,继承于NSObject,可以创建基于矢量的路径.使用此类可以定义常见的圆形.多边形等形状 .我们使用直线.弧(arc) ...

随机推荐

  1. c# 调用 matlab

    如果本机没有安装Matlab 需要安装一个叫 MCRInstaller 的东西  运行环境 如果安装后 需要把D:\MATLAB\R2013a\bin\win64; 写入环境变量 (路径根据实际情况改 ...

  2. Tiled Map地图编辑器键盘快捷键

    Tiled是款不错的地图编辑器,不过快捷键真是隐蔽啊,不看github上得wiki根本不知道,用的过程中查英文文档总是觉得慢,所以翻译成了中文. 通用 右键点击图块(tile):复制图块到图章刷(拖动 ...

  3. 《UML大战需求分析》阅读笔记6

    流程分析三剑客之总结 顺序图,活动图相类似表示活动,状态机图表示状态.分析特点: 顺序图: 强调角色之间的交互,信息明确: 从上到下,从左到右,按时间顺序: 不适合表达复杂特殊情况(循环分支,条件分支 ...

  4. java JFrame窗体真正关闭

    程序: package JFrame.bao; import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent; impor ...

  5. 安装vs2010,vs2015后,删除2015,导致vs2016打不开

    报错:Cannot evaluate the property expression "$([MSBuild]::ValueOrDefault('$(VCTargetsPath)','$(M ...

  6. PerconaXtraBackup --全备增备prepare restore

    Xtrabackup Xtrabackup包含两个主要的工具,即xtrabackup和innobackupex,二者区别如下:  • xtrabackup只能备份innodb和xtradb引擎表,而不 ...

  7. Traceroute命令原理(转)

    Traceroute命令基本功能 该命令用于测试两个TCP/IP系统之间的网络层连通性和显示传输路径中每一跳地址,又称为路径跟踪,如果Traceroute命令测试成功,我们能够观察到从源主机到目的主机 ...

  8. win32 COM组件编写

    win32的com组件: 1. 编辑idl,idl文件会生成对应的com class和com interface,例如 [ object, uuid(xxxxxxxx-xxxx-xxxx-xxxx-x ...

  9. 位图图像处理控件ImageCapture Suite更新至v9.1

    概述:Dynamsoft公司旗下非常出名的位图图像处理控件ImageCapture Suite更新至了v9.1,这次新版本为Mac版本和IE 9新增了不少功能,同时还对其他组件的性能进行了质的提高! ...

  10. Firebird数据库系统的开发团队

    下载Firebird3的发布文档,上面列出了开发团队,仔细看了看,原来俄罗斯人是主导(内核开发),法国人.智利人.巴西人.日本人.荷兰人.捷克人都有.共17人,3人全职. able 13.1. Fir ...