2011-08-10 29 views
1

我想其他的以後做兩個動畫,一個:如何在一排相同的UIImageView執行多個動畫

[UIView beginAnimations:@"growImage" context:nil]; 
[UIView setAnimationDuration:0.2f]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView setAnimationDelegate:self]; 
image6.transform = CGAffineTransformMakeScale(0.9, 0.9); 
[UIView commitAnimations]; 

[UIView beginAnimations:@"shrinkImage" context:nil]; 
[UIView setAnimationDuration:0.5f]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView setAnimationDelegate:self]; 
image6.transform = CGAffineTransformMakeScale(0.1, 0.1); 
[UIView commitAnimations]; 

我知道我可以做第二次動畫

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 

或通過設置

[UIView setAnimationDidStopSelector:@selector(shrinkImage)]; 

但是有沒有我可以在值傳遞(在這種情況下,一個UIImageView命名的圖像的方式6),以便第二個動畫出現在與第一個動畫相同的對象上?

謝謝!

回答

2

如果你想使用基於選擇的方法,您在setAnimationDidStopSelector指定被傳遞您在beginAnimations:context:調用指定context參數選擇。

所以,如果你改變你的beginAnimations來電:

[UIView beginAnimations:@"growImage" context:image6]; 

然後image6將獲得作爲上下文參數的選擇傳遞。

更簡單的方法是使用塊來完成同樣的事情:

[UIView animateWithDuration:0.2 animations:^() { 
    image6.transform = CGAffineTransformMakeScale(0.9, 0.9); 
} 
completion:^(BOOL finished) { 
    [UIView animateWithDuration:0.2 animations:^() { 
     image6.transform = CGAffineTransformMakeScale(0.1, 0.1); 
    }]; 
}]; 
+0

完美 - 謝謝!我也必須改變 - (void)animationDidStop:(NSString *)animationID完成:(NSNumber *)完成上下文:(void *)context {to - (void)animationDidStop:(NSString *)animationID完成:(NSNumber *)完成上下文:(UIImageView *)context { – RanLearns

1

這是我的代碼示例。但你明白了。您需要使用CGAffineTransformConcat將多個動畫拼接在一起。你可以做到這一點 -

[UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
    angle    = 0; 
    scaleFactor  = 1; 
    startPoint.x  = 60.0; 
    startPoint.y  = 60.0; 
    CGAffineTransform scaleTrans = CGAffineTransformMakeScale(scaleFactor, scaleFactor); 
    CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(angle * M_PI/180); 
    boxView.center = startPoint; 
    boxView.transform = CGAffineTransformConcat(scaleTrans, rotateTrans); 
    [UIView commitAnimations]; 

iOS4有Single block animation你可以使用,而不是兩個塊的動畫。蘋果文件指出,單段動畫更加流暢,加上其易於拼接多個動畫一前一後...

UPDATE:更仔細的思考之後,你可以這樣做 -

[UIView animateWithDuration:0.3 
         delay:0 
        options:UIViewAnimationCurveEaseOut 
       animations:^{ /* first level of animation */ 
       } 
       completion:^(BOOL finished){ 
       } 
]; 

正如你所看到的,動畫完成後,你可以開始另一個動畫,或者你可以清理。這種基於塊的動畫,用於鏈動畫在一起......

+0

感謝評論。這看起來像是在縮放和旋轉的同時。我正在尋找增長的形象,然後當動畫完成後,我想縮小圖像縮小 - 我不能同時增長和縮小發生在同一時間,就像你做縮放和旋轉... – RanLearns

+0

我喜歡這個基於塊的動畫的想法,但在使用你的代碼時,我得到一個錯誤 - 不兼容的塊指針類型初始化'void(^)()',預期'void(^)(void)' – RanLearns

+0

注意:自由輸入。可能有錯別字!請參閱Apple文檔或其他在線文檔...很高興我能提供幫助。 –