简体   繁体   English

UIView动画完成回调?

[英]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 . 我想淡出UIView ,然后将其从superView删除。

Animation blocks were introduced in iOS4. 动画块是在iOS4中引入的。 Apple recommends you use these, and the new methods mostly ask for completion blocks which replace callbacks. Apple建议您使用这些,新方法通常会要求使用完成块来代替回调。 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. 当然,不必一定是self ,也不需要那种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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