UIViewPropertyAnimator does not update the view if expected

Here is the Swift code for the playground, which I also tested in the iOS Simulator to make sure it's not a playground issue.

Setting the property fractionComplete

will change the 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 am looking for an 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 these two lines before the first call seems like a workaround for the problem.

animator.startAnimation()
animator.pauseAnimation()

      

I am guessing this is an internal error. rdar: // 30856746

0


source to share





All Articles