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

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

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

// set view properties

[UIView commitAnimations];

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


当前回答

以上这些方法都不能解决我的问题,但是这个方法有帮助:UIView动画会立即设置属性,然后将其动画化。当表示层与模型(set属性)匹配时,它会停止动画。

我解决了我的问题,那就是“我想从你出现的地方动画”(“你”指的是视图)。如果你想要那样,那么:

添加QuartzCore。 CALayer * pLayer = theView.layer.presentationLayer;

将位置设置为表示层

我使用了一些选项,包括UIViewAnimationOptionOverrideInheritedDuration

但由于苹果的文档是模糊的,我不知道它是否真的覆盖其他动画时使用,或只是重置计时器。

[UIView animateWithDuration:blah... 
                    options: UIViewAnimationOptionBeginFromCurrentState ... 
                    animations: ^ {
                                   theView.center = CGPointMake( pLayer.position.x + YOUR_ANIMATION_OFFSET, pLayer.position.y + ANOTHER_ANIMATION_OFFSET);
                   //this only works for translating, but you get the idea if you wanna flip and scale it. 
                   } completion: ^(BOOL complete) {}];

现在这应该是一个不错的解决方案。

其他回答

我的方法是创建一个新的动画到你的终点。设置一个非常短的持续时间,并确保使用+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动画之外。那会停止动画,不管它在哪里,UIView会跳转到你刚刚定义的设置。

如果你通过改变常量而不是视图属性来动画约束,其他方法在iOS 8上都不起作用。

示例动画:

self.constraint.constant = 0;
[self.view updateConstraintsIfNeeded];
[self.view layoutIfNeeded];
[UIView animateWithDuration:1.0f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     self.constraint.constant = 1.0f;
                     [self.view layoutIfNeeded];
                 } completion:^(BOOL finished) {

                 }];

解决方案:

您需要从任何受约束更改影响的视图的层及其子层中删除动画。

[self.constraintView.layer removeAllAnimations];
for (CALayer *l in self.constraintView.layer.sublayers)
{
    [l removeAllAnimations];
}

以上这些方法都不能解决我的问题,但是这个方法有帮助:UIView动画会立即设置属性,然后将其动画化。当表示层与模型(set属性)匹配时,它会停止动画。

我解决了我的问题,那就是“我想从你出现的地方动画”(“你”指的是视图)。如果你想要那样,那么:

添加QuartzCore。 CALayer * pLayer = theView.layer.presentationLayer;

将位置设置为表示层

我使用了一些选项,包括UIViewAnimationOptionOverrideInheritedDuration

但由于苹果的文档是模糊的,我不知道它是否真的覆盖其他动画时使用,或只是重置计时器。

[UIView animateWithDuration:blah... 
                    options: UIViewAnimationOptionBeginFromCurrentState ... 
                    animations: ^ {
                                   theView.center = CGPointMake( pLayer.position.x + YOUR_ANIMATION_OFFSET, pLayer.position.y + ANOTHER_ANIMATION_OFFSET);
                   //this only works for translating, but you get the idea if you wanna flip and scale it. 
                   } completion: ^(BOOL complete) {}];

现在这应该是一个不错的解决方案。

如果你正在寻找灵活的动画,可以根据你的需要开始和停止,使用UIViewPropertyAnimator。

示例代码

let sampleView = UIView()

lazy var animator: UIViewPropertyAnimator = {
    let animator = UIViewPropertyAnimator(duration: 1.2, curve: .easeOut)
    animator.isInterruptible = true
    return animator
}()

// add and your animatable property
animator.addAnimations {
   // what you need to animate add here
   sampleView.alpha = 0
}

// start animation
animator.startAnimation(afterDelay: 0.5)

如果你需要在动画中间停下来。它将在当前状态下停止动画。

// stop animation
if animator.isRunning {
    animator.stopAnimation(true)
}