2012-08-16 127 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及更高版本气馁。如果您使用的是基于块的动画方法,则可以将代理的开始和结束代码直接包含在块中。