【发布时间】:2021-09-29 15:14:58
【问题描述】:
我有一个函数,它遍历 UIButtons 数组并为数组中的每个按钮一个一个地设置动画。目前这一切都很好,直到一个按钮被多次附加到数组中,然后它跳过两个实例的动画。有没有办法阻止这种行为?
func animateButtons() {
disableButtons()
for (index, button) in buttonSequence.enumerated() {
group.enter()
UIButton.animate(
withDuration: 1,
delay: TimeInterval(index),
animations: {
button.backgroundColor = UIColor(red: 63/255, green: 255/255, blue: 0/255, alpha: 1.0)
},
completion: { finished in
button.backgroundColor = UIColor(red: 168/255, green: 61/255, blue: 164/255, alpha: 0.85)
self.group.leave()
}
)
}
group.notify(queue: .main) {
self.enableButtons()
}
}
【问题讨论】:
-
如果你想让按钮一个接一个地动画,为什么不在迭代时给动画添加延迟?
-
这是对调度组的滥用。您应该通过完成处理程序进行递归,或使用关键帧。
标签: arrays swift for-loop animation uibutton