【问题标题】:when i change the priority of constraint then changes are not animating with UpdateconstraintIfNeeded当我更改约束的优先级时,更改不会使用 UpdateconstraintIfNeeded 进行动画处理
【发布时间】:2018-01-17 05:23:44
【问题描述】:

我正在做一个例子,因为我采取了 2 个类似的约束

topconstarints = 150,优先级为 999

top2constraint = 0 优先级为 998

当我改变优先级时

topconstarints.priority = 998

top2constraint.priority = 999

然后我调用 updateConstraintsIfNeeded() 然后它不会对更改进行动画处理,但是如果约束的优先级为 1000,并且如果我更改了该约束,那么它正在工作,所以任何人都可以解释一下这里发生了什么

【问题讨论】:

  • 容器视图只需要调用 layoutIfNeeded
  • 好的,但为什么它不能与 UpdateConstraintsIfNeeded 一起使用,但它的优先级为 1000

标签: ios swift swift3 autolayout


【解决方案1】:

几周前这里有一个类似的question,我正在玩它,看来您不能仅通过更改约束的优先级来为视图设置动画。

如果约束之间的差异仅在于它们的常数,那么您有两种选择。

  1. 只有一个约束,只需更改其常量即可。

  2. 保留两个约束,但始终只激活一个:

    topconstraint = ... // create a constraint 1
    topconstraint.isActive = true // activate it
    top2constraint = ... // also create it and keep it, but don't activate it
    

    然后,当您想要为更改设置动画时:

    self.view.layoutIfNeeded() // just in case there are still some old changes to the autolayout
    
    topconstraint.isActive = false // deactivate the old one
    top2constraint.isActive = true // activate the new one
    self.view.setNeedsLayout() // tell the auto layout that changes have been made
    
    UIView.animate(withDuration: 0.4) {
        // and then finally animate the changes
        self.view.layoutIfNeeded()
    }
    

【讨论】:

    猜你喜欢
    • 2015-09-20
    • 2018-06-12
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 2010-12-24
    相关资源
    最近更新 更多