如何在不冻结 UI 的情况下延迟 for 循环

How to delay a for loop without freezing the UI

我目前正在开发排序可视化工具,但我需要 "run slower" 的 for 循环,因为我想慢慢地可视化算法的工作原理,例如冒泡排序。

这是我的代码

    func bubbleSort(array: inout [Rectangle], view: UIView) {
    for i in 1 ... array.count {
        for j in 0 ..< array.count - i {
            changeRectColor(rect: array[j])
            changeRectColor(rect: array[j+1])

            Thread.sleep(forTimeInterval: 1)


            if (array[j].height > array[j+1].height){
                sortRectColor(rect: array[j])
                sortRectColor(rect: array[j+1])
                Thread.sleep(forTimeInterval: 1)

                rectGenerator.removeRectangleView(view: view, tag: array[j].rectView.tag)
                rectGenerator.removeRectangleView(view: view, tag: array[j+1].rectView.tag)

                let temp = array[j].xPos
                array[j].xPos = array[j+1].xPos
                array[j+1].xPos = temp

                rectGenerator.regenerateRectangleView(rect: &array[j], view: view)
                rectGenerator.regenerateRectangleView(rect: &array[j+1], view: view)


                array.swapAt(j, j+1)
            }

            returnRectColor(rect: array[j])
            returnRectColor(rect: array[j+1])

            Thread.sleep(forTimeInterval: 1)   
        }
    }
}

但如果我这样做,sleep() 会冻结我的 UI,并且不会显示该过程。

我怎样才能做类似的事情而不冻结 UI?

您可以使用计时器 e.x 延迟 1 秒

var counter = 0
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
   guard counter < array.count else { timer.invalidate() ; return }
   // do job
   counter += 1
 }