gpt4 book ai didi

ios - 如何让 subview 在添加后立即出现在 UI 中?

转载 作者:行者123 更新时间:2023-11-28 06:12:04 25 4
gpt4 key购买 nike

本质上,我希望在添加标签时将其显示为 subview ,而不是等到当前线程完成它正在做的事情。在下面的示例中,标 checkout 现在应用程序的 UI 中,同时打印“Awake”。你知道我怎样才能让 subview 在线程休眠之前出现在 UI 中吗?

@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 {} 中并不能解决问题。

谢谢。

最佳答案

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

@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
}

}

关于ios - 如何让 subview 在添加后立即出现在 UI 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com