是否可以在进行UIView
动画时将其取消?还是我必须降至CA级别?
即我已经做了类似的事情(也许也设置了结束动画动作):
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// other animation properties
// set view properties
[UIView commitAnimations];
但是在动画完成之前,我得到了动画结束事件,我想取消它(简称它)。这可能吗?谷歌搜索发现有几个人问相同的问题而没有答案-一两个人猜测这不可能完成。
我这样做的方法是在终点创建一个新的动画。设置很短的持续时间,并确保使用+setAnimationBeginsFromCurrentState:
方法从当前状态开始。将其设置为YES时,当前动画将被剪切。看起来像这样:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.1];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// other animation properties
// set view properties
[UIView commitAnimations];
[UIView setAnimationsEnabled:NO];
// your code here
[UIView setAnimationsEnabled:YES];
didStopSelector
仍会运行。因此,如果您的应用程序中有由动画驱动的逻辑状态,那么您将遇到问题。基于上述原因,我使用UIView
动画的上下文变量。如果通过上下文参数将程序的当前状态传递给动画,则动画停止时,didStopSelector
函数可能会根据当前状态和作为上下文传递的状态值来决定是应该执行操作还是应返回。 >CALayer * pLayer = self.layer.presentationLayer;
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.001 animations:^{
self.frame = pLayer.frame;
}];
UIStackView
动画时,除了removeAllAnimations()
我还需要将一些值设置为初始值,因为removeAllAnimations()
可以将它们设置为不可预测的状态。我有stackView
,其中有view1
和view2
,应该可以看到一个视图,而一个隐藏了:customAnim
用途:
#import <QuartzCore/QuartzCore.h>
.......
[myView.layer removeAllAnimations];
在特定视图上立即停止all动画的最简单方法是:[]:将项目链接到QuartzCore.framework。在代码的开头:
#import <QuartzCore/QuartzCore.h>
现在,当您想要停止视图中所有动画的轨迹消失时,请说这:
[CATransaction begin];
[theView.layer removeAllAnimations];
[CATransaction commit];
中间行本身将完全起作用,但是在运行循环完成之前会有一个延迟(“重绘时刻”)。为避免该延迟,请如图所示将命令包装在显式事务块中。如果当前运行循环中未对该层进行任何其他更改,则此工作有效。
在第二个动画上使用UIViewAnimationOptionBeginFromCurrentState
选项
UIView
动画立即设置属性,然后对其进行动画处理。当表示层与模型(set属性)匹配时,它将停止动画。