gpt4 book ai didi

ios - 打破动画 block 内的循环

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:40 25 4
gpt4 key购买 nike

我试图在完成 UIView 动画后中断 for 循环。这是以下代码段:

public func greedyColoring() {
let colors = [UIColor.blue, UIColor.green, UIColor.yellow, UIColor.red, UIColor.cyan, UIColor.orange, UIColor.magenta, UIColor.purple]

for vertexIndex in 0 ..< self.graph.vertexCount {
let neighbours = self.graph.neighborsForIndex(vertexIndex)
let originVertex = vertices[vertexIndex]

print("Checking now Following neighbours for vertex \(vertexIndex): \(neighbours)")

var doesNotMatch = false

while doesNotMatch == false {
inner: for color in colors{
UIView.animate(withDuration: 1, delay: 2, options: .curveEaseIn, animations: {
originVertex.layer.backgroundColor = color.cgColor
}, completion: { (complet) in
if complet {
let matches = neighbours.filter {
let vertIdx = Int($0)!

print("Neighbour to check: \(vertIdx)")

let vertex = self.vertices[vertIdx-1]

if vertex.backgroundColor == color{
return true
}else{
return false
}
}

//print("there were \(matches.count) matches")

if matches.count == 0 {
// do some things
originVertex.backgroundColor = color
doesNotMatch = true
break inner
} else {
doesNotMatch = false
}
}
})
}
}
}
}

基本上,此方法遍历图形并检查每个顶点及其邻居,并为顶点赋予其邻居均不具有的颜色。这就是为什么它必须在未使用的第一种颜色处中断迭代。我尝试使用 Unlabeled 循环,但它仍然无法编译(break is only allowed inside a loop)。问题是我想可视化哪些颜色已经过测试。这就是我使用 UIView.animate()

的原因

有什么办法可以解决我的问题吗?

最佳答案

您需要了解,您传递给动画函数的完成 block 在动画完成之后被调用,这是您的 for 循环迭代后的很长时间(以计算机时间计)通过颜色数组。您将持续时间设置为 1 秒,这意味着 1 秒后调用完成。由于 for 循环不等待您的动画完成,它们将同时开始动画(可能相差几毫秒)。 for 循环在动画完成之前就已经完成了,这就是为什么中断 for 循环没有意义,因为它不再运行了!

如果你想看到这个,在调用 UIView.animate 函数和 print("Finished") 在完成 block 中。在控制台中,您应该在所有完成之前看到所有的fire

您应该改为对动画进行排队,以便它们一个接一个地开始和结束。

关于ios - 打破动画 block 内的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903376/

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