2012-08-16 50 views
4

動畫結束後,我想做一些動作。如何獲取動畫結束通知

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.80f]; 
self.view.transform = 
CGAffineTransformMakeTranslation(
           self.view.frame.origin.x, 
           480.0f + (self.view.frame.size.height/2) // move the whole view offscreen 
           ); 
[self.view setAlpha:0]; 
[UIView commitAnimations]; 

我做動畫像上面,如何找出動畫結束了,所以我可以做之後,我的行動。

回答

7

使用此:

[UIView animateWithDuration:0.80f animations:^{ 
    self.view.transform = 
    CGAffineTransformMakeTranslation(
            self.view.frame.origin.x, 
            480.0f + (self.view.frame.size.height/2) // move the whole view offscreen 
            ); 
    [self.view setAlpha:0]; 
    } 
    completion:^(BOOL finished){ 
     // your code 
    }]; 
0

[UIView commitAnimations];後面寫上你的代碼。動畫保持在beginAnimationscommitAnimations之間。

+0

@Tejashwar我想用動畫去除視圖,我按照你的說法試過,但是它沒有用動畫去除視圖。 – Newbee 2012-08-16 10:38:44

7

添加到您的動畫:

[UIView setAnimationDidStopSelector:@selector(myAnimationEnded)]; 
[UIView setAnimationDelegate:self]; 

這種方法會告訴你,當它停止;

- (void)myAnimationEnded{ 
    NSLog(@"animation ended"); 
} 
+2

不要忘記'[UIView setAnimationDelegate:self];'。 – 2012-08-16 10:42:54

+2

我更喜歡這種設置動畫屬性的方式,我不喜歡在'{}'之間包裹所有內容。 – 2012-08-16 10:44:30

+0

當然,我的朋友,我只是覺得很明顯,在每次需要互動時將代表設置爲自我。 – 2012-08-16 10:50:33

4

使用UIView-animateWithDuration:animations:completion:類方法。

[UIView animateWithDuration:0.8 animations:^{ 
     CGAffineTransformMakeTranslation(
             self.view.frame.origin.x, 
             480.0f + (self.view.frame.size.height/2) // move the whole view offscreen 
             ); 
     [self.view setAlpha:0]; 

    } completion:^(BOOL finished) { 
     //this block is called when the animation is over 
    }]; 
0

這並不是說setAnimationDelegate

這種方法的使用中的iOS 4.0及更高版本氣餒。如果您使用的是基於塊的動畫方法,則可以將代理的開始和結束代碼直接包含在塊中。