PerformSelectorOnMainThread 未等待完成
我有一个执行一些任务的线程。
最后,我想运行一个选择器来隐藏带有动画的图像。
[self performSelectorOnMainThread:@selector(finishUpdate) withObject:nil waitUntilDone:YES];
我将持续时间设置为 10 秒:
- (void) finishUpdate {
UIImageView *myImage = (UIImageView *)[self.view viewWithTag:788];
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:10.0f];
myImage.frame = CGRectMake(0, 200, 320, 80);
myImage.alpha = 0.0f;
[UIView commitAnimations];
}
但它立即消失,线程立即继续。
你知道如何让我的线程等待或如何显示这个简单的动画吗?
I have a thread which performs some tasks.
At the end I want to run a selector to hide an image with animation.
[self performSelectorOnMainThread:@selector(finishUpdate) withObject:nil waitUntilDone:YES];
I am setting the duration to 10 seconds:
- (void) finishUpdate {
UIImageView *myImage = (UIImageView *)[self.view viewWithTag:788];
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:10.0f];
myImage.frame = CGRectMake(0, 200, 320, 80);
myImage.alpha = 0.0f;
[UIView commitAnimations];
}
But it is disappearing instantly and the thread continues immediately.
Do you know how to make my thread to wait or how to display this simple animation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该方法立即返回是正常行为。
[UIView commitAnimations]
不会等待动画完成(或您使用的任何其他方法)。Kristopher Johnson 是对的,您不应该在这种情况下使用
UIGraphicsGetCurrentContext
。它在drawRect:
中或每当存在有效的图形上下文时使用。动画上下文与图形上下文无关。如果您不需要某些动画委托中的任何上下文,只需传递 NULL 即可。
It is normal behavior for the method to return immediately.
[UIView commitAnimations]
does not wait for the animation to finish (or any other method you use).Kristopher Johnson is right that you shouldn't use
UIGraphicsGetCurrentContext
in this context. It is used indrawRect:
or whenever there is a valid graphics context.The animation context does not relate to a graphics context. Just pass NULL if you don't need any context in some animation delegate.
我没有等待选择器完成,而是使用
sleepForTimeInterval:
方法来暂停我的线程,使用与动画相同的持续时间。Instead of waiting for the selector to finish, I used
sleepForTimeInterval:
method to pause my thread, using the same duration as my animation.