2011-10-14 44 views
10

有沒有一種方法來檢測動畫何時完成?動畫完成後,我想撥打[nav setTitle:navItem]iOS:如何檢測動畫何時完成?

下面是我的代碼片段。希望這個問題很清楚,所以我可以得到一個解決方案,最好是一個例子。

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ 

if(event.subtype == UIEventSubtypeMotionShake){ 


    NSString *imageUrl = @""; 
    NSString *navItem = @""; 

    int randomNumber = arc4random() % 3; 

    switch (randomNumber) { 
     case 0: 
      imageUrl = @"pic1.png"; 
      navItem = @"txt1"; 
      break; 
     case 1: 
      imageUrl = @"pic2.png"; 
      navItem = @"txt2"; 
      break; 
     case 2: 
      imageUrl = @"pic3.png"; 
      navItem = @"txt3"; 
      break; 
     default: 
      break; 
    } 

    animation.animationImages = [NSArray arrayWithObjects: 

           [UIImage imageNamed:@"pic1.png"], 
           [UIImage imageNamed:@"pic3.png"], 
           [UIImage imageNamed:@"pic2.png"], 
           [UIImage imageNamed:@"pic1.png"], 
           [UIImage imageNamed:@"pic2.png"], 
           [UIImage imageNamed:@"pic3.png"], 
           [UIImage imageNamed:imageUrl], 
           nil]; 

    UIImage *img = [UIImage imageNamed:imageUrl]; 
    [imageview setImage:img]; 

    [animation setAnimationRepeatCount:1]; 
    animation.animationDuration = 1; 
    [animation startAnimating]; 

    [nav setTitle:navItem]; 

} 

} 
+2

如果您發現答案可以解決您的問題,那麼接受它並且您獲得2點聲望點是很好的。 – zaph

回答

4

動畫結束時[animation isAnimating] == NO。

如果你要等到它完成,但不會阻止用戶界面:

while ([animation isAnimating]) { 
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]]; 
} 
+0

非常感謝,正是我需要的。問題解決了! :) – thar

+1

@thar:如果這確實解決了問題,「接受」答案:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –

+0

這似乎並不工作了:http://stackoverflow.com/questions/3144300/isanimating-return-is-faulty-for-uiimageview-for-iphone – Nuthinking

10

可以使用setAnimationDidStopSelector委託。
http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html

[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 


- (void)animationDidStop:(NSString*)animationID finished:(BOOL)finished context:(void *)context { 
    /*Do stuff*/ 
} 
+2

我不知道這是否適用於animationImages類型的動畫。僅適用於使用[UIView beginAnimations:context:]的動畫。 – morningstar

+3

它不適用於animationImages類型的動畫。 – Dustin

-2
[self performSelector:@selector(animationFinished) withObject:nil afterDelay:1.0]; 

-(void)animationFinished { 
    // do stuff 
} 

這只是假設,動畫將在時間,你告訴它的量完成。