IOS Animation-CAShapeLayer、UIBezierPath与Animation的结合
在阅读本文之前,对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的结合的更多相关文章
- Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)
1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...
- iOS 动画篇 之 Core Animation (一)
iOS中实现动画有两种方式,一种是自己不断的通过drawRect:方法来绘制,另外一种就是使用核心动画(Core Animation). 导语: 核心动画提供高帧速率和流畅的动画,而不会增加CPU的负 ...
- iOS Swift最简单的Animation
最近发现Animation是一个iOS开发中非常好玩的元素,能给应用的交互性增色不少.比如很多音乐应用的菜单从底部弹出和隐藏的效果. Animation最核心的当然就是UIView的animateWi ...
- iOS 动画效果:Core Animation & Facebook's pop
本文转载至 http://www.cocoachina.com/ios/20151223/14739.html 感谢原创作者分享 前言相信很多人对实现 iOS 中的动画效果都特别头疼,往往懒得动手,功 ...
- IOS 核心动画(Core Animation)
Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它 能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就 可以实现非常强大的功能. Core ...
- iOS 图形图像动画 Core Animation
//Core Animation #define WeakSelf __weak __typeof(self) weakSelf = self #define StrongSelf __strong ...
- iOS关于CAShapeLayer与UIBezierPath的知识内容
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...
- IOS中的动画——Core Animation
一.基础动画 CABasicAnimation //初始化方式 CABasicAnimation * cabase=[CABasicAnimation animation]; //通过keyPath设 ...
- CAShapeLayer + UIBezierPath
UIBezierPath: UIBezierPath是在 UIKit 中的一个类,继承于NSObject,可以创建基于矢量的路径.使用此类可以定义常见的圆形.多边形等形状 .我们使用直线.弧(arc) ...
随机推荐
- 对button或radiobutton制作样式
制作样式在drawable中进行 (cheakbox)在样式中点击变换图片,<selector> <item android:state_checked="true&quo ...
- 确定比赛名次---HDU1285(拓扑排序)
http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目大意: 给你每场比赛的成绩,让你根据成绩把排名弄出来 分析: 本来我是用普通方法写的,然后就一直wa, ...
- ORACLE 自动增长通过封装函数,方便调用
好的编程习惯,是一个很有必要的过程.好的编程习惯,可以因人而异,但是简单地.基本地代码级别的就那些:写注释.合理的缩进.换行.变量命名等. 对我们程序员来说,大部分时间都对着电脑,在对着电脑的大部分时 ...
- [转]MySQL关键性能监控(QPS/TPS)
原文链接:http://www.cnblogs.com/chenty/p/5191777.html 工作中尝尝会遇到各种数据库性能调优,除了查看某条SQL执行时间长短外,还需要对系统的整体处理能力有更 ...
- java中FileInputStream和FileOutputStream对图片操作的例子
package a.ab; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.F ...
- Python爬虫入门案例:获取百词斩已学单词列表
百词斩是一款很不错的单词记忆APP,在学习过程中,它会记录你所学的每个单词及你答错的次数,通过此列表可以很方便地找到自己在记忆哪些单词时总是反复出错记不住.我们来用Python来爬取这些信息,同时学习 ...
- xcode6如何支持空模板
Single View Application 改成空模板的歩揍(xcode6.0.1): 1.删除info.plist 中的Main Storyboard file base name 选项 2.将 ...
- 【leetcode】Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- android handler 简介
android的handler 是一个神奇东西,处理异步消息的时候,我们离不开他.那么他的具体的介绍是什么了? Handler基本概念: Handler主要用于异步消息的处理:当发出一个消息 ...
- android user build serial console
在 init.rc 里有一段 on property:ro.debuggable=1 start console 当user debug时 ro.debuggable=0,console 不会被启动 ...