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) ...
随机推荐
- 1996: [Hnoi2010]chorus 合唱队
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1996 Description Input Output Sample Input 4 1701 ...
- 说说Python中的闭包 - Closure
转载自https://segmentfault.com/a/1190000007321972 Python中的闭包不是一个一说就能明白的概念,但是随着你往学习的深入,无论如何你都需要去了解这么一个东西 ...
- Php 笔记
php基本简介 为何要学习php 通过上网查资料,了解了基本的php知识,并知道了php的优缺点.php是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使用广泛,主要适用 ...
- java读取properties配置文件总结
java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...
- JS 前端格式化JSON字符串工具
JSON格式化工具,简易实现.作为技术宅,直接上代码,供大家使用.前提:一定要引入jquery哦. <!DOCTYPE html> <html lang="en" ...
- 在Windows7 下调试CodeSmith 注意事项
编写CodeSmith模板和编写程序一样,也需要进行调试,CodeSmith支持使用CLR’s Just-in-Time debugger调试模板. 要调试模板,首先要在CodeTemplate声明中 ...
- C代码工具--自动生成enum值和名字映射代码
这年头好像继续做C语言的人不多了,年轻人大多去互联网和移动应用.确实,那两个领域现在来钱快,且总是供不应求.就说刚刚在一个旧同事的微信群里,有人刚放出自己有团队可以做App几分钟,哇塞,好几个人说有项 ...
- struts2中jsp前台传值到后台action的方法(转)
在Struts2中jsp前台传值到action后台的方法 分类: java2012-02-28 13:58 2171人阅读 评论(1) 收藏 举报 actionstrutsjspgetterstrin ...
- linux下subversion server安装手册
linux下subversion server安装手册 安装基于的Linux版本为:Red Hat Enterprise Linux Server release 6.3. 一 准备需要的安装包. ( ...
- sql 注入的防范(一)
为了保证程序的健壮性,我们必须对用户输入的数据做有效性验证,防止用户恶意提交数据. 关于防止 sql 注入 我主要从三个方面入手: 1.确认为正整数的,强制转化为int,$id =$_GET('id ...