这是一个Swift游乐场代码,我也在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