UIViewPropertyAnimator does not update the view wh

2019-08-04 10:31发布

问题:

Here is a Swift playground code, which I also tested in the iOS Simulator just to be sure that it's not an issue of Playground.

Setting the property fractionComplete result in change of state from inactive to active. However as shown below the view is not updated for the first two calls. Only the third call magically updates the view.

This behavior is confusing and I seek for explanation.

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
}

Update: Putting this two lines before the first call seems to be a workaround for the issue.

animator.startAnimation()
animator.pauseAnimation()

I assume it's an internal bug. rdar://30856746