在 iPhone 应用程序中,我尝试使用 setAnimationDidStopSelector 捕获动画结尾。我尝试暂停代码执行直到动画结束。我已经尝试过这个;设置一个全局 BOOL 变量,在提交动画之前将其设置为 TRUE,并在提交动画之后使用 while 循环等待。在 setAnimationDidStopSelector 中,将 BOOL 变量设置为 FALSE 并希望 while 循环中断。但不幸的是,这不起作用,代码甚至没有落入 setAnimationDidStopSelector (我用一些跟踪输出检查)。编辑:如果未添加 BOOL 变量处理,代码将运行到处理程序方法中。
发生动画的代码如下:
self.AnimationEnded=FALSE;
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
// do sth.
[UIView commitAnimations];
while(![self AnimationEnded]);
这也是处理程序的代码:
- (void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
printf("abc\n"); fflush(stdout);
self.AnimationEnded=true;
}
你有什么建议?
最佳答案
在 iOS 4 中,您可以设置完成 block ,而不是使用动画委托(delegate)和处理程序。这是在动画结束时采取操作的一种更简单的方法。如果您不支持 iOS 4 之前的设备,我建议您使用它。
您的示例更改为:
self.animationEnded = NO;
[UIView animateWithDuration:2
animations:^{ /* Do something here */ }
completion:^(BOOL finished){
printf("abc\n");
fflush(stdout);
self.animationEnded = YES;
}];
参见+UIView animateWithDuration:animations:completion:请访问 iOS 开发者网站了解更多信息。
关于iPhone 等待动画结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2726173/