alun-chen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在阅读本文之前,可以看看 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     }

 

可以关注本人的公众号,多年经验的原创文章共享给大家。

posted on 2016-04-10 23:32  alun-chen  阅读(1242)  评论(0编辑  收藏  举报