2016-04-30 15 views
6

私はビューがsView私のViewControllerの中にあります。高さに制約があります - この制約のために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を確認してください –

関連する問題