是否有可能取消一个UIView动画,而它正在进行中?或者我必须降到CA级别?

例如,我做过这样的事情(可能设置了一个结束动画动作):

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// other animation properties

// set view properties

[UIView commitAnimations];

但是在动画完成之前,我得到动画结束事件,我想取消它(缩短它)。这可能吗?在谷歌上搜索一下,会发现一些人问了同样的问题,但没有答案——还有一两个人猜测这是不可能的。


当前回答

CALayer * pLayer = self.layer.presentationLayer;
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.001 animations:^{
    self.frame = pLayer.frame;
}];

其他回答

我也有同样的问题;api没有任何东西来取消某些特定的动画。的

+ (void) setAnimationsEnabled:(BOOL)启用

禁用所有动画,因此不为我工作。有两种解决方案:

1)使你的动画对象成为子视图。然后,当你想要取消该视图的动画时,删除该视图或隐藏它。很简单,但是你需要重新创建没有动画的子视图如果你需要保持它在视图中。

2)只重复一个anim,并在需要时创建一个委托选择器重新启动anim,如下所示:

-(void) startAnimation {
NSLog(@"startAnim alpha:%f", self.alpha);
[self setAlpha:1.0];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(pulseAnimationDidStop:finished:context:)];
[self setAlpha:0.1];
[UIView commitAnimations];
}

- (void)pulseAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if(hasFocus) {
    [self startAnimation];
} else {
    self.alpha = 1.0;
}
}

-(void) setHasFocus:(BOOL)_hasFocus {
hasFocus = _hasFocus;
if(hasFocus) {
    [self startAnimation];
}
}

问题2)总是延迟停止动画,因为它完成当前的动画周期。

希望这能有所帮助。

要取消一个动画,你只需要设置当前正在动画的属性,在UIView动画之外。那会停止动画,不管它在哪里,UIView会跳转到你刚刚定义的设置。

在一个特定的视图上立即停止所有动画的最简单的方法是:

将项目链接到QuartzCore.framework。在代码的开头:

#import <QuartzCore/QuartzCore.h>

现在,当你想停止视图中的所有动画时,这样说:

[CATransaction begin];
[theView.layer removeAllAnimations];
[CATransaction commit];

中线可以自己工作,但是在运行循环结束之前会有一个延迟(“重画时刻”)。为了防止这种延迟,可以将命令包装在显式事务块中,如图所示。如果在当前运行循环中没有对该层执行其他更改,则此工作正常。

很抱歉要恢复这个答案,但在iOS 10中事情有点改变,现在取消是可能的,你甚至可以优雅地取消!

ios10之后,你可以用UIViewPropertyAnimator取消动画!

UIViewPropertyAnimator(duration: 2, dampingRatio: 0.4, animations: {
    view.backgroundColor = .blue
})
animator.stopAnimation(true)

如果你传递true,它会取消动画,并在你取消的地方停止。补全方法将不会被调用。然而,如果你传递了false,你将负责完成动画:

animator.finishAnimation(.start)

你可以完成动画并保持当前状态(.current)或回到初始状态(.start)或结束状态(.end)

顺便说一下,你甚至可以暂停并稍后重新启动……

animator.pauseAnimation()
animator.startAnimation()

注意:如果你不想突然取消,你可以反转你的动画,甚至在你暂停它后改变你的动画!

我的方法是创建一个新的动画到你的终点。设置一个非常短的持续时间,并确保使用+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];