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