Update the constant property of a constraint programmatically in Swift?

女生的网名这么多〃 提交于 2019-12-17 23:26:17

问题


I want to animate an object, so I declare a constraint and add it to the view. I then update the constant property of the constraint inside an UIView animation. Why doesn't this code move the object?

UIView.animateWithDuration(1, animations: {
     myConstraint.constant = 0   
     self.view.updateConstraints(myConstraint)
})

回答1:


In order to declare an animation, you cannot re-define the constraint and call updateConstraints. You are supposed to change the constant of your constraint and follow the format below:

self.view.layoutIfNeeded()
UIView.animateWithDuration(1, animations: {
    self.sampleConstraint.constant = 20
    self.view.layoutIfNeeded()
})


来源:https://stackoverflow.com/questions/28127259/update-the-constant-property-of-a-constraint-programmatically-in-swift

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