开发者

UIView animation completion callback?

开发者 https://www.devze.com 2023-01-06 07:15 出处:网络
Can I setup a function to be called once my animatio开发者_如何转开发n is complete?I want to fade a UIView and then remove it from the superView.Animation blocks were introduced in iOS4. Apple recomme

Can I setup a function to be called once my animatio开发者_如何转开发n 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消