UIViewPropertyAnimator does not update the view when expected
Asked Answered
A

0

1

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

Amaris answered 5/3, 2017 at 11:28 Comment(2)
Looks like an internal bug related to DispatchQueue, for sure. Haven't seen it before. Did you figure it out?Eleemosynary
@DanielLarsson Honestly, I don't think it's a bug in DispatchQueue at least not in the object itself. I only used it to showcase that the fractionComplete property does nothing when its set while being inactive. I assume that Apple might use DispatchQueue internally inside UIViewPropertyAnimator, but for some reason they messed something up, so the fractionComplete property does not work as documented/expected.Amaris

© 2022 - 2024 — McMap. All rights reserved.