如何停止执行FOR循环,直到代码完成
问题描述:
我做了这件事。除了向我自己演示之外,它没有任何其他用途。实质上,我正在移动一个视图,在循环中围绕屏幕创建。问题在于它不会一直等到它完成一个动画才能启动另一个动画,因此视图会绕过前两个并在for循环完成后最后一个toastRect
结束,并且它不会在那。在继续下一步之前,如何强制代码完成每个动画?如何停止执行FOR循环,直到代码完成
int i;
for (i=0; i < 100; i++) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:9];
toast.view.frame = toastRect1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:9];
toast.view.frame = toastRect2;
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:9];
toast.view.frame = toastRect;
[UIView commitAnimations];
NSLog(@"%d",i);
}
}
答
我认为你需要使用+ (void)setAnimationDidStopSelector:(SEL)selector
。如果需要,请仔细查看http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html。
答
可以使用setAnimationDidStopSelector
方法链动画。检查文档的详细信息:http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html%23//apple_ref/occ/clm/UIView/setAnimationDidStopSelector:
谢谢!这使得代码更复杂一些,但它的确有窍门。 – SeniorShizzle 2011-06-17 06:38:30