添加带动画的约束以进行查看

时间:2017-03-17 19:47:14

标签: ios swift constraints nslayoutconstraint

嘿我试图通过更改此代码的约束来引入视图

    UIView.animate(withDuration: 500, animations: {
        self.forgetTopConstraint.isActive = false
        self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
    })

forgetTopConstraint是一个约束,它将forgetPasswordView的顶部固定在view的底部,且常量为0,因此forgetTopConstraint不在视野范围内然后我尝试使用该代码将其内部动画到view的中心,但它突然出现而不是动画

1 个答案:

答案 0 :(得分:1)

如果没有看到更多的周围代码或视图设置,听起来您只需要在包含视图上调用layoutIfNeeded。这是实际执行应用更新约束的工作的方法。 layoutIfNeeded由视图的其他一些更改自动触发,但在想要设置动画的情况下,您实际上不需要在动画块内调用任何其他布局更改。只需调用一个语句,这应该会使它为以前的所有更改设置动画:

self.forgetTopConstraint.isActive = false
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
UIView.animate(withDuration: 500, animations: {
    self.view.layoutIfNeeded()    
})
相关问题