2011-06-13 59 views
5

我的简单的代码:如何在继续之前等到不同课程中的动画完成?

[otherClass doMethodOneWhichHasAnimation]; 

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

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

问题是,它无需等待就推动viewcontroller,我希望它等待。延迟只是延迟整个事情,包括从第一个动画开始。

我怎样才能让它在运行第二位代码之前等待0.8秒?

回答

14

动画完成

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

      // PushView 

     }]; 

后< iOS 4以上版本看看setAnimationDidStopSelector可以按下

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

您可以使用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]; 
+0

我这个工作的爱,但它不为我用工作(这是执行滚动任何动画表视图更新后的一行)。 – 2014-02-11 10:18:25

+0

@StianHøiland尝试将'layoutIfNeeded'发送到事务内部的表视图(我的示例发送'doMethodOneWhichHasAnimation')。 – 2014-02-11 19:25:13

+0

不幸的是没有运气。 – 2014-02-12 09:44:22

0

我通过制作一个新的类来运行动画来解决这个问题。

该课程计算与动画开始同步传递的时间。

您的时间内提供了动画类和两个动画块和类反发生在这个数字。

当计数器已经走到了尽头,你知道在动画也完成了。

...所有没有随机复杂的图书馆。

-2

只需调用与延迟时间间隔

喜欢 - 一个方法

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

这个答案是非常错误的。你只是在这里要求比赛条件。 – 2013-07-17 19:02:16

相关问题