2016-04-30 46 views
6

我在ViewController中查看sView。它的高度有限制 - 我为此限制创建了IBOutlet - sViewHeightConstraint。我想用动画降低sView的高度。使用Swift动画查看高度

我创建功能

UIView.animateWithDuration(5.5, animations: { 
       self.sViewHeightConstraint.constant = 50 
      }) 

高度的观点正在发生变化,但我没有看到任何动画。我做错了什么?

回答

27

使用layoutIfNeeded()

view.layoutIfNeeded() // force any pending operations to finish 

UIView.animateWithDuration(0.2, animations: {() -> Void in 
    self.sViewHeightConstraint.constant = 50 
    self.view.layoutIfNeeded() 
}) 

迅速3

view.layoutIfNeeded() 
sViewHeightConstraint.constant = 50 

UIView.animate(withDuration: 1.0, animations: { 
    self.view.layoutIfNeeded() 
}) 
+0

谢谢!你能解释一下 - 这个代码的含义是什么? – moonvader

+0

@moonvader'layoutIfNeeded'在视图上强制任何挂起的操作完成,然后在动画块中捕获帧更改。欲了解更多信息,请致电 –

+0

。检查http://stackoverflow.com/questions/1182945/how-is-layoutifneeded-used –