[英]iOS - multiple animation blocks?
我有多個動畫必須作為鏈運行。 我處理這個問題的方法是使用 completionHandler 並運行下一個 animation 塊。 有沒有更清潔的方法來處理這個問題?
[UIView animateWithDuration:1 animations^{
// perform first animation
}completion:(BOOL finished){
[UIView animateWithDuration:1 animations^{
// perform second animation
}completion:(BOOL finished){
}];
}];
您還可以使用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
上創建一個類別,它采用一組這樣的塊並為您將它們鏈接在一起,但是您必須處理所有極端情況。 你如何定義時間? 你如何處理中止的動畫等等。在大多數情況下,以上可能是最好的方法。
我寫了一篇關於這個的博客文章。 我的方法是創建一個 animation object 來封裝塊和其他 animation 屬性。 然后,您可以將此類對象的數組傳遞給另一個 animation object,后者將按順序執行它們。 該代碼可在GitHub上獲得。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.