UIView动画完成后执行代码?

Jum*_*hyn 2 iphone objective-c uiview

动画结束后执行代码的最佳方法是什么(例如,在淡出后从超视图中删除视图)?我明白setAnimationDidStopSelector:但不确定如何使用它.

Max*_*Max 13

这很简单.你必须定义像这样的方法

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
//do smth
}
Run Code Online (Sandbox Code Playgroud)

然后打电话

[UIView beginAnimations: nil context: nil];
[UIView setAnimationDelegate: self]; //or some other object that has necessary method
[UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
Run Code Online (Sandbox Code Playgroud)

您可以发布要删除的视图.为此你必须这样做:

[UIView beginAnimations: nil context: someView];
Run Code Online (Sandbox Code Playgroud)

然后添加你的回调:

   -(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
        [(UIView*)context removeFromSuperView];
    }
Run Code Online (Sandbox Code Playgroud)