2012-04-04 55 views
3

我有多個動畫必須作爲鏈運行。 我一直在處理這個問題的方法是使用completionHandler並運行下一個動畫塊。 有沒有更清晰的方法來處理這個問題?iOS - 多個動畫塊?

[UIView animateWithDuration:1 animations^{ 

     // perform first animation 
    }completion:(BOOL finished){ 

      [UIView animateWithDuration:1 animations^{ 

       // perform second animation 
      }completion:(BOOL finished){ 

     }]; 

}]; 
+0

您可以使用塊來獲得非常乾淨的結果。請參閱:http://xibxor.com/objective-c/uiview-animation-without-nested-hell/ – 2013-04-04 18:34:03

回答

10

您還可以使用animateWithDuration:delay:options:animations:completion:交錯的延遲,讓他們按順序啓動,但通常這是最好的完成塊做這些事。

如果有幾個和它使代碼難以閱讀,只需分解出的塊(我打字這個從我的頭頂,所以它可能不會編譯):

typedef void (^AnimationBlock)(); 

AnimationBlock firstAnimation = ^{ ... }; 
AnimationBlock secondAnimation = ^{ ... }; 

[UIView animateWithDuration:1 animations:firstAnimation completion:(BOOL finished) { 
    [UIView animateWithDuration:1 animations:secondAnimation];}]; 

你可以在UIView上創建一個類別,並將它們連接在一起,然後再處理所有的角落案例。你如何定義時機;你如何處理流產的動畫等等。以上可能是大多數情況下最好的方法。

1

我寫了一個blog post這個。我的方法是創建一個封裝塊和其他動畫屬性的動畫對象。然後,您可以將這些對象的數組傳遞給另一個將按順序執行它們的動畫對象。該代碼可在GitHub上找到。