UIActivityIndicatorView not showing until after loading done

て烟熏妆下的殇ゞ 提交于 2019-12-12 08:33:03

问题


I have a button on the currently navigated to viewcontroller, connected to an IBAction.

In the IBAction I create a UIActivityIndicatorView as usual, with [self.view addSubView], then load some pictures.

I've tried setNeedsDisplay on the indicator view, the view controller, and the window, but it still loads the pictures before showing the indicator, which of course is quite useless to me.

So I'm looking for a way to either force an instant redraw (which when I think a little more about it is unlikely to make work), or a way to load the pictures after the indicator has appeared, or a way to launch a separate thread or similar to start animating / show the indicator, or put the indicator in a separate viewcontroller and somehow force it to add/show itself before going on to the picture-loading.

Recommendations?


回答1:


What I do in this situation is spawn a new thread, which frees up the main thread to handle UI interaction while stuff is loading in the background.

First show the UIActivityIndicatorView, then spawn a new thread that loads the images, then on the last line of the method that is executed in the new thread, hide the UIActivityIndicatorView.

Here's an example:

//do stuff...
[activityIndicatorView startAnimating];
[NSThread detachNewThreadSelector:@selector(loadImages) toTarget:self withObject:nil];

In your loadImages method:

- (void) loadImages {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   //load images...
   [activityIndicatorView performSelectorOnMainThread:@selector(stopAnimating)];
   [pool drain];
}


来源:https://stackoverflow.com/questions/4097961/uiactivityindicatorview-not-showing-until-after-loading-done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!