我在 iOS 上有以下函数在 TableView 上执行 UI 工作,挑战是如果它开始花费比 0.5 秒更长的时间。应向用户显示微调器,以使屏幕看起来不会像卡住一样。
func updateForm(with rowItems: [RowItem]) {
self.tableView.beginUpdates() // performance tweak.
let viewControllerName = String.init(describing: self.classForCoder) // id
var defaultSection = Form.createSectionWith(tag: viewControllerName, in: form)
// MARK: - update rows
let allRows = self.form.allRows
let startTime = CFAbsoluteTimeGetCurrent()
var showSpinner = false
for (index, item) in rowItems.enumerated() {
.
.
.
<TableView processing work on 100's of rows>
.
.
.
// Evaluate our running time for this process loop, and display spinner if we're past a threshold of seconds.
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
if timeElapsed > 0.5 && showSpinner == false {
showSpinner = true
self.showActivityIndicator(withStatus: "processing") // {NEVER GETS DISPLAYED}
}
} // for (index, item) ...
当然,当我调用 showActivityIndicator
时,它从未真正显示出来。
如何中断和暂停 UI 工作以显示 showActivityIndicator
微调器动画,然后让循环继续?
最佳答案
您需要离开主线程,以便让运行循环循环并发生重绘时刻。在继续您的工作之前显示微调器之后的简单延迟将解决问题。但是您将不得不重新组织代码以允许这样做。最简单的解决方案是放弃 0.5 秒的想法,在开始之前只显示微调器。
关于ios - 如何在 UI 工作中显示微调器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547552/