添加子视图后,如何使它立即出现在UI中? [英] How do I make subview appear in UI immediately after adding it?

查看:73
本文介绍了添加子视图后,如何使它立即出现在UI中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我希望标签在添加时显示为子视图,而不是等到当前线程完成其操作后再显示.在下面的示例中,标签在打印唤醒"的同时出现在应用程序的用户界面中.您知道如何让子视图在线程休眠之前显示在UI中吗?

Essentially I want a label to appear as a subview when I add it and not wait until the current thread has finished what it is doing. In the below example, the label appears in the UI of the app at the same time 'Awake' is printed. Do you know how I can get the subview to appear in the UI before the thread sleeps?

@IBAction func ButtonTapped(_ sender: Any) {

    let label = UILabel(frame: self.view.bounds)
    label.text = "Label Text"
    self.view.addSubview(label) // I want this to appear on the UI before...
    sleep(3)
    print("Awake") // ... this is printed

}

将addSubView()放在DispatchQueue.main.async {}中并不能解决问题.

And enclosing the addSubView() line within DispatchQueue.main.async {} doesn't fix the issue.

谢谢.

推荐答案

感谢Paulw11,我发现我需要在后台线程上进行处理.快速的简单解决方案:

Thanks to Paulw11, I figured out that I need to do the processing on a background thread. Simple solution in swift:

@IBAction func ButtonTapped(_ sender: Any) {

    let label = UILabel(frame: self.view.bounds)
    label.text = "Label Text"
    self.view.addSubview(label) // I want this to appear on the UI before...

    DispatchQueue.global(qos: .background).async {
        sleep(3)
        print("Awake") // ... this is printed
    }

}

这篇关于添加子视图后,如何使它立即出现在UI中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆