如何取消UIView基于块的动画? [英] How to cancel UIView block-based animation?

查看:115
本文介绍了如何取消UIView基于块的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了大量的SO内容和Apple的参考资料,但仍然无法解决我的问题。

I've searched loads of SO stuff and in Apple's references, but still unable to manage my problem.


  1. 屏幕,其中2个UIImageViews与它们连接2个UIButtons

  2. 2种动画:

    2.1 。第一个是按比例放大,然后逐个放下每个图像,一个接一个,只在viewDidLoad
    2.2中一次。第二个,当按下按钮(这是自定义按钮,隐藏在每个UIImageView的'内部')时,它会触发相应UIImageView的动画 - 只有一个,而不是两个 - (也可以向上扩展,然后向下)。

  3. 当我为iOS4 +写作时,我被告知要使用基于块的动画!



我需要什么:



如何取消正在运行的动画?
除了最后一个,我设法取消了...:/
这是我的代码片段:

What I need:

How to cancel running animation? I've managed to cancel after all but the last one... :/ Here is my code snippet:

[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");
                                      }];
            }];
        }];
    }];

不知何故,smallLetter UIImageView无法正常工作,因为按下时(通过按钮)bigLetter正在取消动画...
提前感谢:)

Somehow the smallLetter UIImageView is not working properly, beacuse when pressed (through button) bigLetter is canceling animations properly... thanks in advance :)

编辑:
我使用过这个解决方案,但仍然有缩小smallLetter UIImageView的问题 - 根本没有取消...
解决方案

EDIT2:我在开头添加了这个next / prev方法:

I've added this at the beginning of next/prev methods:

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

问题停留...:/不知道如何中断上一个动画的字母在动画链中

problem stays... :/ no idea how to interrupt last animation for letters in animation chain

推荐答案

您可以通过以下方式停止视图上的所有动画:

You can stop all animations on a view by calling:

[view.layer removeAllAnimations];

(你需要导入QuartzCore框架来调用view.layer上的方法)。

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

如果你想停止一个特定的动画,而不是所有动画,你最好的选择是明确使用CAAnimations而不是UIView动画助手方法,那么你将有更精细的控制并且可以通过名称显式停止动画。

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.

可以在此处找到Apple Core动画文档:

The Apple Core Animation documentation can be found here:

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

这篇关于如何取消UIView基于块的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆