有时动画执行时没有动画效果(零时间)。 在下面的代码中有时会打印“Time = 0.0000002”
let date = Date()
UIView.animate(withDuration: 0.2, animations: {
}) { (finish) in
print("Time = \(Date().timeIntervalSince(date))")
}
最佳答案
这是因为您没有为 animations
block 内的任何内容设置动画,因此这是正常行为。您可以使用以下代码进行验证,
let date = Date()
self.view.layer.opacity = 0.5
UIView.animate(withDuration: 2.0, animations:
self.view.layer.opacity = 1.0
}) { (finish) in
print("Time = \(Date().timeIntervalSince(date))")
}
animations
block 的目的是在给定时间为属性的开始值和结束值差异设置动画。一旦属性值达到animations
block 中的给定值,它将立即调用finish
block 。
如果您没有任何动画效果,只想在几秒钟后回调,最好使用 Timer
或 DispatchQueue
例如,
Timer(timeInterval: 0.2, repeats: false) { timer in
// Do whatever you want
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
// Do whatever you want
}
关于ios - 查看动画有时会在零时间内执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49828762/