UIView动画完成回调?

13

我能否设置一个函数,在我的动画完成后自动调用它?我希望淡出一个UIView,然后将其从superView中移除。

2个回答

37

iOS4中引入了动画块。苹果公司建议使用它们,而新方法大多要求使用完成块来替换回调函数。例如:

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

25

没问题,很简单:

当你配置动画时

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

然后像这样定义你的方法:

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

当然,这个方法并不一定非得是self


谢谢 - 我浏览了所有的方法,真不敢相信我错过了那个。 - Slee
1
哈哈,老派了^^去看下一个答案吧,这个已经不再使用了。 - Macistador

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接