2011-11-24 47 views
0

我已經是動畫圖像的順序如下:IOS核心動畫,當我的動畫序列結束時如何調用animationDidStop:方法?

IBOutlet UIImageView *block; 
NSArray *image_arr = [NSArray arrayWithObjects:[UIImage imageNamed:@"img1"],[UIImage imagedNamed:@"img2"],nil]; 
block.animationImages = image_arr; 
block.animationDuration =1; 
block.animationRepeatcount =5; 
[block startAnimating]; 

而且方法animationDidStop:

-(void)animationDidStop:(NSString*)animationID finished:(BOOL)didFinish context:(void*)context{ 
NSLog(animationID); 
} 

什麼我需要做的,有一個名爲在動畫的最後方法animationDidStop ?

回答

0

我需要做什麼才能使動畫結束時調用animationDidStop方法?

你不能讓它調用,因爲這不是核心動畫。你有思想混爲一談兩個不同的東西:

  • 具有一個UIImageView做的非常頭腦簡單的卡通風格的動畫顯示的一連串圖像的能力;

  • 核心動畫,其中可以有一個完成塊或完成處理程序。

我建議你做的是既可以使用真正核心動畫,你實際上可以通過延遲性能回調到自己設置的動畫和完成塊或處理,否則只是假的當這個僞動畫結束時,從現在起5秒鐘的代碼。

+0

下面是我書中描述所需區別的章節,並依次描述每個動畫技術:http://www.apeth.com/iOSBook/ch17.html – matt

0

了最新的iOS版本4.2及更高版本,你應該使用模塊用於動畫

下面將推動你在正確的方向,嘗試改變UIViewAnimationOption到不同的東西:

[UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionRepeat animations:^{ 
       //Animation here 
      }completion:^(BOOL finished) { 
       if(finished){ 
        //Your Code here 
       } 
      } 
1

的UIImageView的「 animationImages「屬性與coreanimation無關,它只是觸發內部NSTimer切換圖像。

-(void)animationDidStop:finished:context: 

選擇器被稱爲核心動畫的一部分。

我會在你的情況下,嘗試什麼是這樣的:

IBOutlet UIImageView *block; 
NSArray *image_arr = [NSArray arrayWithObjects:[UIImage imageNamed:@"img1"],[UIImage imagedNamed:@"img2"],nil]; 
block.animationImages = image_arr; 
block.animationDuration = animationDuration; 
block.animationRepeatcount = animationRepeatCount; 
[block startAnimating]; 
[self performSelector:@selector(animationFinished:) withObject:@"someAnimationId" afterDelay: animationDuration * animationRepeatCount]; 

請記住,這種方法可能會導致一定的滯後性 - 如果動畫幀的圖像顯示,第一次作爲一個組成部分在動畫開始播放的第一次之前,會延遲加載,並且可能會影響performSelector調用的精度。

如果事實證明這是一個問題check out this answer's forceLoad method,如果這不起作用在隱藏的UIImageView上調用startAnimation,它對我來說只是一次。