我想为 uiviews 的 alpha 设置动画,但我似乎无法做到。它的行为很奇怪,并说运行 UI 更改不建议在后台线程上运行,但我不知道如何让它在主线程上运行。有人可以帮助我吗?我相信它会跳过第一个 UIView.animate 块并在没有任何动画的情况下执行第二个块。
func animateSemaphore() {
circleRed.alpha = 0.2
circleOrange.alpha = 0.2
circleGreen.alpha = 1
let dispatchSemaphore = DispatchSemaphore(value: 0)
let dispatchQueue = DispatchQueue.global(qos: .background)
dispatchQueue.async {
UIView.animate(withDuration: 0.5, delay: 5, options: .curveEaseInOut) {
self.circleOrange.alpha = 1
self.circleGreen.alpha = 0.2
} completion: { (_) in
print("1")
dispatchSemaphore.signal()
}
dispatchSemaphore.wait()
UIView.animate(withDuration: 0.5, delay: 3, options: .curveEaseInOut) {
self.circleOrange.alpha = 0.2
self.circleRed.alpha = 1
} completion: { (_) in
dispatchSemaphore.signal()
}
dispatchSemaphore.wait()
UIView.animate(withDuration: 0.5, delay: 5, options: .curveEaseInOut) {
self.circleOrange.alpha = 1
} completion: { (_) in
dispatchSemaphore.signal()
}
dispatchSemaphore.wait()
UIView.animate(withDuration: 0.5, delay: 1, options: .curveEaseInOut) {
self.circleOrange.alpha = 0.2
self.circleRed.alpha = 0.2
self.circleGreen.alpha = 1
} completion: { (_) in
self.animateSemaphore()
}
}
}