首页 > 代码库 > animateWithDuration:animations:completion:
animateWithDuration:animations:completion:
Creates an animation block object that can be used to set up keyframe-based animations for the current view.
Declaration
(void)animateKeyframesWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
Parameters
duration
The duration of the overall animation, measured in seconds. If you specify a negative value or 0, changes are made immediately and without animations.
delay
Specifies the time (in seconds) to wait before starting the animation.
options
A mask of options indicating how you want to perform the animations. For a list of valid constants, see “UIViewKeyframeAnimationOptions”.
animations
A block object containing the changes to commit to the views. Typically, you call the addKeyframeWithRelativeStartTime:relativeDuration:animations: method one or more times from inside this block. You may also change view values directly if you want those changes to animate over the full duration. This block takes no parameters and has no return value. Do not use a nil value for this parameter.
completion
A block object to be executed when the animation sequence ends. This block has no return value and takes a single Boolean argument that indicates whether or not the animations finished before the completion handler was called. If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle. You can use a nil value for this parameter.
我一直不明白最后的completion什么用,参考中的回答者大概回答了。
意思是说你可以当你中断或者取消了一个动画,里面的completion会为FALSE。你要清楚,虽然取消了这个动画,但是completion block回调函数仍然会执行。如果你连接了动画序列,你想让这个序列停止的话,你只需要继续之前已经完成的动画。
如果你写一个游戏,预期是炸弹从屏幕飞过。那么你有一个动画来让炸弹移动,你的completion block写的是另外一个动画来展示炸弹爆炸,然后下面你可能调用一些方法来减分或者其他。
如果玩家点击了炸弹,你应该取消炸弹的移动动画,然后炸弹安然无恙的飞过。你之前的completion仍然会执行,所以你应该知道动画已经被执行结束或者被取消。
参考:http://stackoverflow.com/questions/8686922/what-exactly-does-bool-parameter-do-in-animatewithdurationanimationscompletion
animateWithDuration:animations:completion: