(iphone) 当 UI 更新较多时,如何显示活动指示器?

发布于 2024-11-03 02:02:33 字数 1014 浏览 6 评论 0 原文

我尝试在大量 UI 更新时显示活动指示器。

我最初失败的尝试是

  1. 在视图 1 处显示活动指示器
  2. pushViewController(创建此viewController的视图需要很长时间)
  3. 当view1的viewDidDisappear(当pushViewController的push动画时 发生),隐藏活动指示器

问题是,即使我在pushViewController之前调用了“显示活动指示器”,活动指示器也没有显示,因为在创建被推送的viewController的视图时发生了繁重的UI。

我改变了策略,如下所示,它有效,但我不明白为什么。

  1. pushViewController
  2. 在 viewController 的 viewDidLoad 处显示活动指示器
  3. 在 viewDidAppear 处进行繁重的 UI 加载并隐藏指示器

我印象中,第一种方法没有显示指示器的原因是由于繁重的 UI 过程。
请参阅链接中的讨论。 (iphone) 在大量 ui 更新之前显示活动指示器

如果也是队列上的大量 UI 更新导致 UI 更新延迟(指示器未显示),为什么第二种方法会有所不同?

我知道 viewDidLoad 仅在创建视图时才会被调用。
当视图实际显示在屏幕上时,viewDidAppear 就会被调用。(因此可以被调用多次)

但是是否有一个根本的区别可以解释为什么使用 viewDidAppear 的第二种方法在我的情况下有效?

还有除了所谓的主线程之外还有UI更新线程吗? 或者主线程是UI更新线程?

谢谢

I tried to show activity indicator while heavy UI update.

My initial failed attempt was

  1. show activity indicator at view1
  2. pushViewController (creating view of this viewController takes a long time)
  3. when view1's viewDidDisappear(when pushViewController's push animation
    occurs), hide activity indicator

Problem was even though I did call "show activity indicator" before pushViewController, activity indicator wasn't showing because of heavy UI which occurred in creating the view of viewController which was being pushed.

I changed strategy as bellow and it works and I don't get why.

  1. pushViewController
  2. show activity indicator at viewDidLoad of viewController
  3. do the heavy UI loading at viewDidAppear and hide indicator

I was under impression the reason that the indicator didn't show up with the first method was due to heavy UI process.
Please refer to the discussion at link.
(iphone) showing activity indicator before heavy ui updating

If too much UI update on queue caused delayed UI update(indicator not showing up), why the 2nd method makes a difference?

I know that viewDidLoad gets called only when view gets created.
And viewDidAppear gets called when view actually shows up on screen.(hence can be called multiple times)

But is there a fundamental difference which would explain why the 2nd approach of using viewDidAppear works in my situation?

And is there a UI update thread apart from the so-called main thread?
Or main thread is the UI update thread?

Thank you

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

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

发布评论

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

评论(1

逆夏时光 2024-11-10 02:02:33

首先,我想确保您正在主线程上更新 UIKit UI。这是必须的,在使用 UIKit 时您不必指定线程,除非您说使用主线程,但我认为这是多余的。

我将在 viewWillAppear 中启动活动指示器。然后在 viewDidLoad 中加载其他所有内容。

First I want to make sure that you're updating UIKit UI on the main thread. This is a must, you shouldn't have to specify a thread when working with UIKit, unless you say to use the main thread, but I think that would be redundant.

I would start the activity indicator in viewWillAppear. Then load everything else in viewDidLoad.

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