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

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

问题描述

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

我有什么:

  1. 一个有 2 个 UIImageViews 和 2 个 UIButtons 连接到它们的屏幕
  2. 2种动画:

    1. viewDidLoad
    2. 中一个接一个地放大然后缩小每个图像
    3. 当按下按钮(每个 UIImageView 的内部"隐藏的自定义按钮)时,它会触发适当的 UIImageView 动画——只有一个,而不是两个——(也放大,然后向下).
    4. 当我为 iOS4+ 编写代码时,我被告知要使用基于块的动画!

我需要什么:

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

这是我的代码片段:

[UIImageView animateWithDuration:2.0延迟:0.1选项:UIViewAnimationOptionAllowUserInteraction动画:^{isAnimating = 是;self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 2.0, 2.0);}完成:^(BOOL完成){如果(!完成)返回;[UIImageView animateWithDuration:2.0延迟:0.0选项:UIViewAnimationOptionAllowUserInteraction动画:^{self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 0.5, 0.5);}完成:^(BOOL完成){如果(!完成)返回;[UIImageView animateWithDuration:2.0延迟:0.0选项:UIViewAnimationOptionAllowUserInteraction动画:^{self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 2.0, 2.0);}完成:^(BOOL完成){如果(!完成)返回;[UIImageView animateWithDuration:2.0延迟:0.0选项:UIViewAnimationOptionAllowUserInteraction动画:^{self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 0.5, 0.5);}完成:^(BOOL完成){如果(!完成)返回;//块字母按钮[self.bigLetterButton setUserInteractionEnabled:YES];[self.smallLetterButton setUserInteractionEnabled:YES];//NSLog(@"vieDidLoad 动画完成");}];}];}];}];

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

我已经使用了这个解决方案,但是在缩小 smallLetter UIImageView 时仍然有问题 - 根本没有取消......解决方案

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

- (void)stopAnimation:(UIImageView*)source {[UIView animateWithDuration:0.01延迟:0.0选项:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction)动画:^ {source.transform = CGAffineTransformIdentity;}完成:NULL];}

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

解决方案

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

[view.layer removeAllAnimations];

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

如果您想停止特定动画,而不是所有动画,最好的办法是显式使用 CAAnimations 而不是 UIView 动画辅助方法,然后您将获得更精细的控制,并且可以按名称显式停止动画.

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

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

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

What I have:

  1. A screen with 2 UIImageViews and 2 UIButtons connected to them
  2. 2 kinds of animation:

    1. Scaling up and then down of each image, one after another, only once in viewDidLoad
    2. When a button pressed (a custom button hidden 'inside' of each UIImageView) it triggers animation of appropriate UIImageView–only one, not both–(also scale up, then down).
    3. As I am writing for iOS4+ I'm told to use block based animations!

What I need:

How do I cancel a 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");
                                      }];
            }];
        }];
    }];

Somehow the smallLetter UIImageView is not working properly, because when pressed (through button) bigLetter is canceling animations properly...

EDIT: I've used this solution, but still having problem with scaling down smallLetter UIImageView - not cancelling at all... solution

EDIT2: 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];

(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

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

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