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];
}
但是它立即消失,线程立即继续。 您知道如何让我的线程等待或如何显示此简单动画吗?     

解决方法

该方法立即返回是正常行为。
[UIView commitAnimations]
不等待动画完成(或您使用的任何其他方法)。 克里斯托弗·约翰逊(Kristopher Johnson)是正确的,在这种情况下您不应使用
UIGraphicsGetCurrentContext
。在
drawRect:
中或存在有效图形上下文时使用。 动画上下文与图形上下文无关。如果不需要某些动画委托中的任何上下文,只需传递NULL。     ,我没有等待选择器完成,而是使用
sleepForTimeInterval:
方法暂停线程,使用与动画相同的持续时间。
[NSThread sleepForTimeInterval:0.3];