Sai*_*een 7 uiview uiviewanimation ios swift
我试图在完成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
}
}
})
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
基本上这个方法迭代一个Graph并检查每个顶点及其邻居,并给顶点一个颜色,它的邻居都没有.这就是为什么它必须打破未使用的第一种颜色的迭代.我试图使用Unlabeled循环,但它仍然没有编译(break is only allowed inside a loop).问题是我想要想象哪些颜色已经过测试.这就是为什么我使用的UIView.animate()
无论如何都有解决我的问题?
您需要了解,传递给 animate 函数的完成块是在动画完成后调用的,这是在 for 循环迭代颜色数组之后很长一段时间(以计算机时间计算)。您将持续时间设置为 1 秒,这意味着 1 秒后调用完成。由于 for 循环不会等待动画完成,因此它们将同时开始动画(可能会相差几毫秒)。for 循环在动画完成之前就已完成,这就是为什么打破 for 循环没有意义,因为它不再运行!
如果您想看到这一点,请在调用函数print("Fire")之前和完成块中添加一个调用。在控制台中,您应该在所有完成之前看到所有的火。UIView.animateprint("Finished")
相反,您应该对动画进行排队,以便它们依次开始和结束。
| 归档时间: |
|
| 查看次数: |
598 次 |
| 最近记录: |