UIViewPropertyAnimator does not update the view when expected

隐身守侯 提交于 2019-12-11 04:31:09

问题


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

来源:https://stackoverflow.com/questions/42607833/uiviewpropertyanimator-does-not-update-the-view-when-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!