ctivityIndi​​catorView直到加载完成后才显示

ctivityIndi​​catorView直到加载完成后才显示

本文介绍了UIActivityIndi​​catorView直到加载完成后才显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,目前导航到viewcontroller,连接到IBAction。

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

在IBAction我创建一个UIActivityIndi​​catorView通常,[self.view addSubView ],然后加载一些图片。

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

我尝试了setNeedsDisplay在指示器视图,视图控制器和窗口,但它仍然加载图片之前显示指示器,这当然对我来说是没用的。

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.

推荐?

推荐答案

我在这种情况下做的是产生一个新的线程,释放主线程来处理UI交互,而东西在后台加载。

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.

首先显示 UIActivityIndi​​catorView ,然后生成一个加载图像的新线程,然后在新线程中执行的方法的最后一行,隐藏 UIActivityIndi​​catorView

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.

以下是一个示例:

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

loadImages 方法中:

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

这篇关于UIActivityIndi​​catorView直到加载完成后才显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:03