IOS Animation-CAKeyframeAnimation例子(簡單動畫實現)

在閱讀本文以前,能夠看看 CABasicAnimation的例子html

也能夠看看IOS Animation-CABasicAnimation、CAKeyframeAnimation詳解&區別&聯繫post

 

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圓周(圓圈)運動url

 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     }

 

能夠關注本人的公衆號,多年經驗的原創文章共享給你們。spa

相關文章
相關標籤/搜索