小编典典

试图快速动画约束

swift

我有一个UITextField我想在点击时扩大它的宽度。我设置了约束,并确保左侧的约束的优先级低于我要尝试在右侧进行动画的优先级。

这是我尝试使用的代码。

  // move the input box
    UIView.animateWithDuration(10.5, animations: {
        self.nameInputConstraint.constant = 8
        }, completion: {
            (value: Bool) in
            println(">>> move const")
    })

这可行,但是似乎只是瞬间发生,似乎没有任何动作。我尝试将其设置为10秒以确保没有遗漏任何东西,但得到的结果相同。

nameInputConstraint是我控制拖动以从IB连接到我的类的约束的名称。

谢谢您的帮助!


阅读 186

收藏
2020-07-07

共1个答案

小编典典

您需要先更改约束,然后为更新设置动画。

self.nameInputConstraint.constant = 8

迅捷2

UIView.animateWithDuration(0.5) {
    self.view.layoutIfNeeded()
}

斯威夫特3,4,5

UIView.animate(withDuration: 0.5) {
    self.view.layoutIfNeeded()
}
2020-07-07