【问题标题】:How do i wait until an animation in a different class is finished before continuing?我如何等到不同班级的动画完成后再继续?
【发布时间】:2011-09-13 21:01:03
【问题描述】:

我有简单的代码:

[otherClass doMethodOneWhichHasAnimation];

并且该动画持续 0.8 秒。接下来是推送视图控制器的代码:

SchemeView *schemeView = [[SchemeView alloc] init];
    [self.navigationController pushViewController:schemeView animated:YES];
    [schemeView release];

问题是,它无需等待即可推动视图控制器,我希望它等待。做延迟只会延迟整个事情,包括开始的第一个动画。

如何让它在运行第二段代码之前等待 0.8 秒?

【问题讨论】:

    标签: iphone objective-c uiviewanimation


    【解决方案1】:

    动画完成后可以推送

    [UIView animateWithDuration:0.8
            animations:^{ 
                // Animation
            } 
            completion:^(BOOL finished){
    
              // PushView
    
            }];
    

    [self performSelector:@selector(metohodToPushView) withObject:nil afterDelay:0.8];
    

    【讨论】:

      【解决方案2】:

      您可以使用CATransaction 在任何动画之后运行完成块,而无需更改创建动画的方法的源代码。你需要link with the QuartzCore framework,你需要#import <QuartzCore/QuartzCore.h>。然后你可以这样做:

      [CATransaction begin]; {
      
          [CATransaction setCompletionBlock:^{
              // This block runs after any animations created before the call to
              // [CATransaction commit] below.  Specifically, if
              // doMethodOneWhichHasAnimation starts any animations, this block
              // will not run until those animations are finished.
      
              SchemeView *schemeView = [[SchemeView alloc] init];
                  [self.navigationController pushViewController:schemeView animated:YES];
                  [schemeView release];
          }];
      
          // You don't need to modify `doMethodOneWhichHasAnimation`.  Its animations are
          // automatically part of the current transaction.
          [otherClass doMethodOneWhichHasAnimation];
      
      } [CATransaction commit];
      

      【讨论】:

      • 我希望它能够工作,但它不适用于我的使用(即在任何动画表格视图更新后执行滚动到一行)。
      • @StianHøiland 尝试将layoutIfNeeded 发送到事务内的表视图(我的示例发送doMethodOneWhichHasAnimation)。
      • 不幸的是没有运气。
      【解决方案3】:

      我通过创建一个新类来运行动画解决了这个问题。

      这个类计算与动画开始同步的时间。

      您将动画的时间输入到类中,动画块和类计数器都会输入该数字。

      当计数器结束时,您就知道动画也结束了。

      ...所有这些都没有随机复杂的库。

      【讨论】:

        【解决方案4】:

        只需调用具有延迟时间间隔的方法

        喜欢-

        [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.00];
        

        【讨论】:

        • 这个答案是非常错误的。您只是在这里询问比赛条件。
        猜你喜欢
        • 2015-10-03
        • 2013-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-07
        • 2014-12-17
        相关资源
        最近更新 更多