开发者

UIActivityIndicatorView not showing until after loading done

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 addSubV开发者_开发知识库iew], 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?


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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜