在另一个线程中运行MBProgressHUD?

编程入门 行业动态 更新时间:2024-10-23 01:41:21
本文介绍了在另一个线程中运行MBProgressHUD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 MBProgressHUD 在填充UITableView时显示忙动画以使用用户。 UITableView 阻塞主线程,因此动画甚至不会出现,直到表完成加载。

I am using MBProgressHUD to display a "busy" animation to use user while a UITableView is being populated. The UITableView blocks the main thread so the animation does not even appear until the table finishes loading.

在 UITableView 占用主线程的情况下,让另一个线程上的忙碌动画运行。

Is there a way to make the busy animation run on another thread while the UITableView occupies the main thread?

推荐答案

UIKit在运行循环完成当前循环时绘制图形。换句话说,如果您要配置视图(例如,MBProgressHUD),更改将不会显示,直到下一个运行循环迭代。因此,如果你不允许运行循环通过阻塞主线程来旋转,UI更改不会立即出现。

UIKit does its drawing when the run loop completes the current cycle. In other words, if you're configuring a view (e.g., MBProgressHUD), the changes won't be visible until the next run loop iteration. Thus if you don't allow the run loop to spin by blocking the main thread, the UI changes won't appear immediately.

如果您无法在后台线程上进行工作,则需要允许运行循环完成其循环,然后再开始长时间运行的阻塞任务主线程。

If you can't do your work on a background thread, you need to allow the run loop to complete its cycle before you start your long-running blocking task on the main thread.

您可以通过在下一个运行循环迭代中安排执行时间来执行此操作。

You can do this by scheduling execution on the next run loop iteration.

// Setup and show HUD here [self performSelector:@selector(myTask) withObject:nil afterDelay:0.001];

在myTask结束时隐藏HUD。 或者您可以手动运行运行循环。

Hide the HUD at the end of myTask. Or you can run the run loop manually.

// Setup and show HUD here [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]]; // Insert myTask code here // Hide the shown HUD here

$ b b

或者(如果可以使用块)

Or (if you can use blocks)

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){ // Insert myTask code here });

更多推荐

在另一个线程中运行MBProgressHUD?

本文发布于:2023-11-11 23:33:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579847.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   MBProgressHUD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!