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

时间:2012-03-05 16:09:14

标签: iphone ios animation ios4 uiimageview

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

我拥有的:

  1. 屏幕,其中2个UIImageViews与它们连接2个UIButtons
  2. 2种动画:
    2.1。第一个是向上扩展,然后在viewDidLoad中一个接一个地向下移动每个图像 2.2。第二个,当按下按钮(这是自定义按钮,隐藏在每个UIImageView的'内部')时,它会触发相应UIImageView的动画 - 只有一个,而不是两个 - (也可以向上扩展,然后向下)。
  3. 当我为iOS4 +写作时,我被告知要使用基于块的动画!
  4. 我需要什么:

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

    [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正在取消动画... 提前谢谢:)

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

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

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

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

4 个答案:

答案 0 :(得分:138)

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

[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

答案 1 :(得分:32)

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

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic">
  <style>
   .md-fab-toolbar.md-fab-bottom-right {
      width: 100vw;
    }
   </style>
</head>

<body ng-app="BlankApp" ng-controller="BasicDemoCtrl" layout="column" ng-cloak>   
    <!-- Page Window -->
    <md-content flex>
        <div style="position:absolute;left:48px;top:115px;width:1507px;height: 500px;"></div>
    </md-content>
     <md-fab-toolbar md-direction="left" class="md-fab-bottom-right">
        <md-fab-trigger class="align-with-text">
            <md-button aria-label="menu" class="md-fab md-primary">
                <md-icon>menu</md-icon>
            </md-button>
        </md-fab-trigger>
        <md-toolbar>
            <md-fab-actions class="md-toolbar-tools">
                <md-button class="md-icon-button" ng-click="reviewTable()">
                    <md-icon>launch</md-icon>
                </md-button>
                <md-button class="md-icon-button" ng-click="showActionToast()">
                    <md-icon>edit</md-icon>
                </md-button>
                <md-button class="md-icon-button">
                    <md-icon>arrow_back</md-icon>
                </md-button>
            </md-fab-actions>
        </md-toolbar>
    </md-fab-toolbar>
     
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
    <!-- Your application bootstrap  -->
    <script type="text/javascript" src="app.js"></script>
</body>

</html>

答案 2 :(得分:2)

我想补充尼克的答案,让removeAllAnimations顺利进入下一个想法非常方便。

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

答案 3 :(得分:0)

您可以尝试此操作(在Swift中):

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

注意:如有必要,您可以在这两个调用之间放置代码,例如:

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