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

rai*_*lin 74 iphone animation uiimageview ios4 ios

我搜索了很多SO的东西和Apple的参考资料,但仍然无法解决我的问题.

是)我有的:

  1. 屏幕与2 UIImageViews nad连接2 UIButtons
  2. 2种动画:
    2.1.第一个是向上扩展,然后在viewDidLoad 2.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");
                                      }];
            }];
        }];
    }];
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

问题保持...:/不知道如何中断动画链中字母的最后一个动画

Nic*_*ood 141

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

[view.layer removeAllAnimations];
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

  • 只想添加,在我自己的观察中,后续animateWithDuration在调用removeAllAnimations后不再起作用.这是为了进行分页和拉动以刷新控件,也许其他人可能会观察到不同的东西.我最终使用CABasicAnimation并调用[myView.layer removeAnimationForKey:@"animationKey"]; (9认同)

Har*_*war 35

对于iOS 10,使用UIViewPropertyAnimator进行动画处理.它提供了启动,停止和暂停UIView动画的方法.

 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)
Run Code Online (Sandbox Code Playgroud)