2016-02-12 37 views
0

我已经看了四周,但我一直未能找到这个简单的问题的答案。有没有办法让Swift CABasicAnimation动画的效果永久化? (意思是当动画结束后,视图不会重置到它在动画开始之前的状态。)迅速使动画效果永久

+0

你应该fillModes https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CAMediaTiming_protocol/index.html#/看看/ apple_ref/doc/constant_group/Fill_Modes –

回答

1

有没有一种方法,使雨燕CABasicAnimation动画永久的效果

绝对如此。只需将您正在动画的属性设置为动画结束时的值即可。您可以在创建和添加动画的同时执行此操作。 (您可能会想打开隐动画关闭当你做到这一点,这样你就不会动画这个改变。)

例子从我自己的代码(我动画的transform一个名为arrow使其旋转层):

let startValue = arrow.transform 
let endValue = CATransform3DRotate(
    startValue, CGFloat(M_PI)/4.0, 0, 0, 1) 
// change the layer, without implicit animation 
// THIS IS WHAT YOU ARE ASKING ABOUT 
CATransaction.setDisableActions(true) 
arrow.transform = endValue 
// construct the explicit animation 
let anim = CABasicAnimation(keyPath:"transform") 
anim.duration = 0.8 
anim.fromValue = NSValue(CATransform3D:startValue) 
anim.toValue = NSValue(CATransform3D:endValue) 
// ask for the explicit animation 
arrow.addAnimation(anim, forKey:nil) 
+0

有关此代码背后原理的完整讨论,请参阅我的书:http://www.apeth.com/iOS书/ ch17.html#_using_a_cabasicanimation – matt