[英]Animate NSLayoutConstraint created programmatically
Something special has to be done to animate a constraint instantiated programmatically? 需要做一些特殊的事情来动画化以编程方式实例化的约束吗?
I instantiate a constraint programmatically as a lazy variable: 我以编程方式将约束实例化为惰性变量:
private lazy var topConstraint: NSLayoutConstraint = {
let constraint = view.topAnchor.constraint(equalTo: otherView.topAnchor, constant: otherView)
return constraint
}()
Then, I animate a change in the constant of this constraint: 然后,我为该约束的常数设置动画:
topConstraint.constant = newValue
view.setNeedsLayout()
UIView.animate(withDuration: 1) {
self.view.layoutIfNeeded()
}
It is not working... I also have tried putting the constant setting into the block: 它不起作用...我也尝试将常量设置放入块中:
UIView.animate(withDuration: 1) {
self.topConstraint.constant = newValue
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
But I achieved nothing at all, I've tried almost all the combinations. 但是我什么也没做,我尝试了几乎所有的组合。 The constraint constant is properly changed, but always without animation. 约束常数已正确更改,但始终没有动画。
PD: I also gave a shot to UIViewPropertyAnimator
. PD:我还给UIViewPropertyAnimator
。
No need for setNeedsLayout()
. 无需setNeedsLayout()
。
self.topConstraint.constant = 0.0
UIView.animate(withDuration: 1.0) {[weak self] in
self?.topConstraint.constant = 10.0
self?.view.layoutIfNeeded()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.