在阅读本文之前,对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. Ajax Step By Step2

    第二.[$.get()和$.post()方法] .load()方法是局部方法(有需要父$),因为他需要一个包含元素的 jQuery 对象作为前缀.而$.get()和 $.post()是全局方法,无须指 ...

  2. 网站内容禁止复制和粘贴、另存为的js代码(转)

    1.使右键和复制失效 方法1: 在网页中加入以下代码: 代码如下: <script language="Javascript"> document.oncontextm ...

  3. Some About Spring

    什么是Spring:Spring是一个从实际开发中抽取出来的框架,它对代码中需要重复解决的步骤抽象成为了一个框架.留给开发者的仅仅是与特定应用相关的部分,大大提高了企业应用的开发效率.例外.Sprin ...

  4. git 命令大全

    git init # 初始化本地git仓库(创建新仓库) git config --global user.name "xxx" # 配置用户名 git config --glob ...

  5. Jade之Plain Text

    Plain Text jade提供了3种得到纯文本的方法. Piped Text 添加纯文本的一个最简单的方法就是在文本最前面加|符号即可. jade: p | It must always be o ...

  6. home键通知

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("closeAlert"), n ...

  7. iOS 取绝对值函数

    1.abs(a) 处理int类型a的取绝对值 2.fabsf(a)  处理float类型a的取绝对值 3.fabs(a)   处理double类型a的取绝对值

  8. 【分块打表】bzoj3798 特殊的质数

    块大小为10^5. #include<cstdio> using namespace std; const int table[] = {0, 4784, 8978, 12981, 169 ...

  9. C#交错数组

    交错数组本质上是一维数组只不过这个一维数组里的元素都是数组.因为该一维数组里面的元素可以是任何大小的数组所以定义时只能以这样的格式定义 ][]; 定义完后必须对该数组进行初始化后方可进行赋值等操作否则 ...

  10. 神经网络hopfield的学习

    Hopfield神经网络使用说明. 该神经网络有两个特点: 1,输出值只有0,1 2,Hopfield没有输入(input) 这里解释一下第二个特点,什么叫没有输入?因为在使用Hopfield网络的时 ...