首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何取消UIView基于块的动画?

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

Stack Overflow用户
提问于 2012-03-06 00:09:14
回答 4查看 59.9K关注 0票数 90

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

我所拥有的:

  1. 由2个UIImageView和2个UIButton连接的屏幕
  2. 2种动画:
    1. 放大然后缩小每个图像,一个接一个,在viewDidLoad
    2. When中只按下一次按钮(隐藏在每个UIImageView内部的自定义按钮)它会触发相应的<UIImageView>D12的动画(也可以放大,然后缩小)。
    3. 当我在为iOS4+编写代码时,我被告知要使用基于块的D12

我需要的是:

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

下面是我的代码片段:

代码语言:javascript
运行
复制
[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时会正确地取消动画...

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

EDIT2:我在next/prev方法的开头添加了以下内容:

代码语言:javascript
运行
复制
- (void)stopAnimation:(UIImageView*)source {
    [UIView animateWithDuration:0.01
                          delay:0.0 
                        options:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction)
                     animations:^ {
                         source.transform = CGAffineTransformIdentity;
                     }
                     completion:NULL
     ];
}

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-03-13 07:57:04

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

代码语言:javascript
运行
复制
[view.layer removeAllAnimations];

(您需要导入QuartzCore框架才能在view.layer上调用方法)。

如果要停止特定动画,而不是所有动画,最好的方法是显式使用CAAnimations,而不是UIView动画辅助方法,这样您将拥有更精细的控制,并且可以按名称显式停止动画。

可以在以下位置找到Apple Core动画文档:

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

票数 159
EN

Stack Overflow用户

发布于 2016-12-31 04:20:23

对于iOS 10,使用UIViewPropertyAnimator设置动画。它提供了启动、停止和暂停UIView动画的方法。

代码语言:javascript
运行
复制
 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)
票数 65
EN

Stack Overflow用户

发布于 2014-10-20 07:35:15

我会补充尼克的答案,使removeAllAnimations平滑下一个想法是非常方便的。

代码语言:javascript
运行
复制
[view.layer removeAllAnimations];
[UIView transitionWithView:self.redView
                  duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                      [view.layer displayIfNeeded];
                  } completion:nil];
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9569943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档