2017-03-17 80 views
1

嘿,我試圖把一個視圖中有變化的是約束與此代碼添加約束動畫,查看

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

沒有看到更多的周圍的代碼或視圖設置,它聽起來像你只需要撥打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()  
}) 
+0

我離完全相同的答案5秒鐘,直到「沒有看到更多的代碼」。好答案。 – dfd

+0

謝謝,我之前做過,但在'forgetPasswordView':D –