2011-04-01 193 views
2

我在想什麼最好的方法是實現一系列不同的動畫。如果我調用以下方法並嘗試對同一個對象進行右,左和上移動,例如,它將不起作用,因爲編譯器沒有以線性方式處理它們,並且最終得到了我的對象簡單地往上走(跳過步驟走左,右):iphone連續動畫

-(IBAction)clickStart 
{ 
    [self Animation1]; 
    [self Animation2]; 
    [self Animation3]; 

} 

現在我所能,這一點,但它在某種程度上感覺繁瑣,怪我。考慮這是動畫1的方法:

[pageShadowView setFrame:CGRectMake(100, 0, CGRectGetWidth(pageShadowView.frame), CGRectGetHeight(pageShadowView.frame))]; 

     [UIView beginAnimations:@"Animation1" context:nil]; // Begin animation 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(Animation2)]; 

    [pageShadowView setFrame:CGRectMake(200, 0, CGRectGetWidth(pageShadowView.frame), CGRectGetHeight(pageShadowView.frame))]; 

    [UIView commitAnimations]; // End animations 

然後我可以做同樣的Animation2,即調用動畫3一旦完成。但說實話,這是非常混亂,不是一個非常明確的編碼方法。無論如何,要獲得像我在開始時所建議的那樣的「線性」代碼(這是行不通的),還是我只需要使用選擇器方法?

感謝您的任何建議!

回答

10

如果你沒有支持iOS的< 4:

[UIView animateWithDuration:0.2 animations:^{ 
    // animation 1 
} completion:^(BOOL finished){ 
    [UIView animateWithDuration:0.2 animations:^{ 
     // animation 2 
    } completion^(BOOL finished){ 
     [UIView animateWithDuration:0.2 animations:^{ 
      // animation 3 
     }]; 
    }]; 
}] 
+0

非常感謝。儘管一見鍾情,但我認爲這實際上比選擇器方法做得更清楚。至少我有代碼以線性方式呈現。謝謝! – 2011-04-01 10:21:07

+1

塊意味着您不必在通常會導致意大利麪代碼的課程級別存儲動畫狀態。 – mxcl 2011-04-01 11:56:54

2

我們創建了一個組件,用於鏈接聲明使用的塊(CPAnimationSequence on Github)動畫步驟。

它給你非常可讀的代碼,這樣的事情:

[[CPAnimationSequence sequenceWithSteps: 
    [CPAnimationStep   for:0.25 animate:^{ self.imageView.alpha = 0.0; }], 
    [CPAnimationStep   for:0.25 animate:^{ self.headline.alpha = 0.0; }], 
    [CPAnimationStep   for:0.25 animate:^{ self.content.alpha = 0.0; }], 
    [CPAnimationStep after:1.0 for:0.25 animate:^{ self.headline.alpha = 1.0; }], 
    [CPAnimationStep   for:0.25 animate:^{ self.content.alpha = 1.0; }], 
    nil] 
runAnimated:YES]; 

相比於通常的基於塊的方法(由Max很好的描述),它的意圖和其代表性匹配的優勢:一線性序列的動畫步驟。我們在an article on our iOS development blog中詳細闡述了此主題。