我正在使用的应用程序中有一个函数,与其他操作相比,它有时需要相对较长的时间。我希望在执行该功能时出现一个图像,向用户显示该应用程序仍在正常工作。
我认为可以这样做的方式是:
_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;
...
}
...
}
}
有没有办法强制更新当前视图,或者有没有更好的办法在函数执行时创建"加载"弹出窗口?
在执行此操作时不应该阻塞UI,我认为如果只在单独的线程中调用需要很长时间的方法会更好。
绘制直到运行循环结束才发生;没有办法使它发生在代码的中间。您可以将对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
});