UIActivityIndi​​catorView 在加载完成后才显示

发布于 2024-09-30 07:31:10 字数 358 浏览 7 评论 0原文

我在当前导航到视图控制器上有一个按钮,连接到 IBAction。

在 IBAction 中,我像往常一样使用 [self.view addSubView] 创建一个 UIActivityIndi​​catorView,然后加载一些图片。

我已经在指示器视图、视图控制器和窗口上尝试过 setNeedsDisplay,但它仍然在显示指示器之前加载图片,这对我来说当然毫无用处。

因此,我正在寻找一种方法来强制立即重绘(当我多想一点时,它不太可能起作用),或者一种在指示器出现后加载图片的方法,或者一种启动单独的线程或类似的开始动画/显示指示器,或者将指示器放在单独的视图控制器中,并以某种方式强制它在继续图片加载之前添加/显示自身。

建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦萦几度 2024-10-07 07:31:10

在这种情况下,我所做的就是生成一个新线程,这会在后台加载内容时释放主线程来处理 UI 交互。

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

这是一个示例:

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文