简体   繁体   中英

UIView animation completion callback?

Can I setup a function to be called once my animation is complete? I want to fade a UIView and then remove it from the superView .

Animation blocks were introduced in iOS4. Apple recommends you use these, and the new methods mostly ask for completion blocks which replace callbacks. For example:

[UIView animateWithDuration:0.5f
                      delay:0.0f
                    options:UIViewAnimationCurveEaseInOut
                 animations:^{
                   [myView setAlpha:0.0f];
                 }
                 completion:^(BOOL finished) {
                   [myView removeFromSuperview];
                 }]; 

Yes, that's easy:

When you configure your animation

[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(myAnimationStopped:finished:context:)];

And define your method like:

-(void)myAnimationStopped:(NSString *)animationID 
                 finished:(NSNumber *)finished
                  context:(void *)context {
   // fancy code here
}

Doesn't have to be self and that method, of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM