码迷,mamicode.com
首页 > 移动开发 > 详细

IOS Animation-CAKeyframeAnimation例子(简单动画实现)

时间:2016-04-11 00:10:47      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

在阅读本文之前,可以看看 CABasicAnimation的例子

也可以看看IOS Animation-CABasicAnimation、CAKeyframeAnimation详解&区别&联系

 

1)让一个layer左右晃动

 1     //让一个layer左右晃动
 2     func addLayerKeyframeAnimationRock(layer:CALayer) {
 3         let animation = CAKeyframeAnimation(keyPath: "position.x")
 4         animation.values = [0, 10, -10, 0]
 5         //additive设置为true是使Core Animation 在更新 presentation layer 之前将动画的值添加到 model layer 中去。可以看到上面的values是0,10,-10,0. 没有设置的话values=layer.position.x+0, layer.position.x+10, layer.position.x-10
 6         animation.additive = true
 7         animation.duration = 0.3
 8         animation.repeatCount = 999999
 9         
10         layer.addAnimation(animation, forKey: "addLayerKeyframeAnimationRock")
11     }

 

2)让一个layer圆周(圆圈)运动

 1     //让一个layer圆周(圆圈)运动
 2     func addLayerKeyframeAnimationOrbit(layer:CALayer) {
 3         let animation = CAKeyframeAnimation(keyPath: "position")
 4         let boundingRect = CGRectMake(layer.frame.origin.x, layer.frame.origin.y, 100, 100)
 5         animation.path = CGPathCreateWithEllipseInRect(boundingRect,nil)
 6         animation.duration = 3
 7         animation.repeatCount = HUGE
 8         //其值为kCAAnimationPaced,保证动画向被驱动的对象施加一个恒定速度,不管路径的各个线段有多长,并且无视我们已经设置的keyTimes
 9         animation.calculationMode = kCAAnimationPaced
10         //kCAAnimationRotateAuto,确定其沿着路径旋转(具体要自己来体验,这里难解释)
11         animation.rotationMode = kCAAnimationRotateAuto
12         
13         layer.addAnimation(animation, forKey: "addLayerKeyframeAnimationOrbit")
14     }

 

IOS Animation-CAKeyframeAnimation例子(简单动画实现)

标签:

原文地址:http://www.cnblogs.com/alunchen/p/5376529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!