如何取消基于 UIView 块的动画?

我已经在苹果的参考资料中搜索了大量 SO 的内容,但仍然无法解决我的问题。

我所拥有的:

  1. 一个有两个 UIImageView和两个 UIButton连接的屏幕
  2. 2种动画:
    1. viewDidLoad中,每个图像先放大后放小,一个接一个,只放大一次
    2. 当一个按钮按下(一个自定义按钮隐藏在每个 UIImageView’内’) ,它触发适当的 UIImageView动画-只有一个,而不是两个-(也放大,然后向下)。
    3. 当我为 iOS4 + 写作时,我被告知要使用基于块的动画!

我需要:

我如何取消一个正在运行的动画? 我已经设法取消了所有,但最后一个... :/

下面是我的代码片段:

[UIImageView animateWithDuration:2.0
delay:0.1
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
isAnimating = YES;
self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 2.0, 2.0);
} completion:^(BOOL finished){
if(! finished) return;
[UIImageView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 0.5, 0.5);
} completion:^(BOOL finished){
if(! finished) return;
[UIImageView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 2.0, 2.0);
} completion:^(BOOL finished){
if(! finished) return;
[UIImageView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 0.5, 0.5);
}
completion:^(BOOL finished){
if (!finished) return;
//block letter buttons
[self.bigLetterButton setUserInteractionEnabled:YES];
[self.smallLetterButton setUserInteractionEnabled:YES];
//NSLog(@"vieDidLoad animations finished");
}];
}];
}];
}];

不知为何,smallLetterUIImageView是不正常工作,因为当按下(通过按钮) bigLetter是取消动画正确..。

编辑: 我已经使用了这个解决方案,但仍然有问题,缩小 smallLetter UIImageView-不取消在所有..。 解决方案

编辑2: 我在 next/prev 方法的开头添加了这个:

- (void)stopAnimation:(UIImageView*)source {
[UIView animateWithDuration:0.01
delay:0.0
options:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction)
animations:^ {
source.transform = CGAffineTransformIdentity;
}
completion:NULL
];
}

问题仍然存在... :/不知道如何中断动画链中的字母最后一个动画

63449 次浏览

You can stop all animations on a view by calling:

[view.layer removeAllAnimations];

(You'll need to import the QuartzCore framework to call methods on view.layer).

If you want to stop a specific animation, not all animations, your best best bet is to use CAAnimations explicitly rather than the UIView animation helper methods, then you will have more granular control and can stop animations explicitly by name.

The Apple Core Animation documentation can be found here:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html

I'd add to Nick's answer that to make removeAllAnimations smooth next idea be very handy.

[view.layer removeAllAnimations];
[UIView transitionWithView:self.redView
duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[view.layer displayIfNeeded];
} completion:nil];

For iOS 10 use UIViewPropertyAnimator to animate. It provides methods to start, stop and pause UIView animations.

 let animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeOut){
        self.view.alpha = 0.0
}
// Call this to start animation.
animator.startAnimation()


// Call this to stop animation.
animator.stopAnimation(true)

You can try this (in Swift):

UIView.setAnimationsEnabled(false)
UIView.setAnimationsEnabled(true)

Note: you can put code between those two calls if necessary, for example:

UIView.setAnimationsEnabled(false)
aview.layer.removeAllAnimations() // remove layer based animations e.g. aview.layer.opacity
UIView.setAnimationsEnabled(true)