Xcode7からXcode8にアップデートしていて既存コードの動作に違いがあったんで備忘録。
UIView(self)を画面の下から出し入れするアニメーションを実装するとして
【Xcode7 - Swift2】
制約の値を設定して、selfに対してlayoutIfNeeded()
で問題なくアニメーションされていた
///
/// アニメーション表示
///
self.constraintBottom.constant = 0
UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.layoutIfNeeded()
}, completion: nil)
///
/// アニメーション非表示
///
self.constraintBottom.constant = -self.frame.size.height
UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.layoutIfNeeded()
}, completion: nil)
【Xcode8 - Swift3】
制約の値を設定して、selfに対してlayoutIfNeeded()
をやってもアニメーションされない
Stack Overflow によるとsuperview
に対してlayoutIfNeeded()
を呼びなさいとのこと
http://stackoverflow.com/questions/39489925/swift-3-uiview-animation
///
/// アニメーション表示
///
self.constraintBottom.constant = 0
UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.superview?.layoutIfNeeded()
}, completion: nil)
///
/// アニメーション非表示
///
self.constraintBottom.constant = -self.frame.size.height
UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.superview?.layoutIfNeeded()
}, completion: nil)
無事アニメーションされました