0

我正在使用的应用程序中有一个功能,与其他操作相比,它有时需要相对较长的时间。我希望在函数执行时出现图像,以向用户显示应用程序仍在正常工作。

我认为可以做到的方式是:

_checkImpossibleImage.hidden = NO;
bool ratioIsPossible = [PaintGame isPossible:_paintChipRatio:_paintCanRatios];
_checkImpossibleImage.hidden = YES;

本质上,它会将图像设置为可见,执行函数,然后将图像设置为不可见。但是,在执行完本节中的所有代码之前,视图似乎不会更新。以下是整体功能:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0){
        // Do nothing (cancel option selected)
    } else {
        if (_buttonKey == @"New") {

            _checkImpossibleImage.hidden = NO;
            bool ratioIsPossible = [PaintGame isPossible:_paintChipRatio:_paintCanRatios];
            _checkImpossibleImage.hidden = YES;

            ...
        }
    ...
    }
}

有没有办法强制更新当前视图,或者有没有更好的方法在函数执行时创建“加载”弹出窗口?

4

2 回答 2

0

这样做时你不应该阻塞 UI,我认为如果你只是在单独的线程中调用需要很长时间的方法会更好。

于 2012-07-18T00:22:23.573 回答
0

直到运行循环结束时才会绘制;没有办法让它发生在你的代码中间。您可以将调用延迟到isPossible::(顺便说一下,这是一个糟糕的方法名称),直到循环的下一次复飞,方法是将其放在主调度队列中:

_checkImpossibleImage.hidden = NO;
dispatch_async(dispatch_get_main_queue(), ^{
        bool ratioIsPossible = [PaintGame isPossible:_paintChipRatio:_paintCanRatios];
        _checkImpossibleImage.hidden = YES;
        // More code to deal with the value of ratioIsPossible
});
于 2012-07-18T00:32:18.973 回答