这是一个 Swift Playground 代码,我还在 iOS 模拟器中对其进行了测试,以确保它不是 Playground 的问题。
设置属性fractionComplete
会导致状态从inactive
变为active
。但是,如下所示,前两个调用的视图没有更新。只有第三次调用神奇地更新了视图。
这种行为令人困惑,我寻求解释。
let liveView = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 50))
liveView.backgroundColor = .white
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = liveView
let square = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
square.backgroundColor = .red
liveView.addSubview(square)
let animator = UIViewPropertyAnimator.init(duration: 5, curve: .easeIn)
animator.addAnimations {
square.frame.origin.x = 350
}
// State: inactive
// Does nothing, position is at 0.0
DispatchQueue.main.async {
animator.fractionComplete = 0.5
}
// State: active
// Stil does nothing, position is at 0.0
DispatchQueue.main.async {
animator.fractionComplete = 0.5
}
// Updates from 0.0 to 0.75 - Why now?
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
animator.fractionComplete = 0.75
}
更新:将这两行放在第一次调用之前似乎是解决该问题的方法。
animator.startAnimation()
animator.pauseAnimation()
我认为这是一个内部错误。rdar://30856746