取消 UIView 动画? [英] Cancel a UIView animation?
问题描述
是否可以在 UIView
动画进行中取消它?还是我必须降到 CA 级别?
Is it possible to cancel a UIView
animation while it is in progress? Or would I have to drop to the CA level?
即我做过这样的事情(也许也设置了结束动画动作):
i.e. I've done something like this (maybe setting an end animation action too):
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// other animation properties
// set view properties
[UIView commitAnimations];
但是在动画完成并且我收到动画结束事件之前,我想取消它(缩短它).这可能吗?谷歌搜索发现有几个人在问同样的问题却没有答案——还有一两个人推测这是不可能的.
But before the animation completes and I get the animation ended event, I want to cancel it (cut it short). Is this possible? Googling around finds a few people asking the same question with no answers - and one or two people speculating that it can't be done.
推荐答案
我的做法是为您的终点创建一个新动画.设置一个非常短的持续时间并确保使用 +setAnimationBeginsFromCurrentState:
方法从当前状态开始.当您将其设置为 YES 时,当前动画将被缩短.看起来像这样:
The way I do it is to create a new animation to your end point. Set a very short duration and make sure you use the +setAnimationBeginsFromCurrentState:
method to start from the current state. When you set it to YES, the current animation is cut short. Looks something like this:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.1];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// other animation properties
// set view properties
[UIView commitAnimations];
这篇关于取消 UIView 动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!