2011-06-13 20 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

相關問題