LoginSignup
14
12

More than 5 years have passed since last update.

swift3でのUIView.animateの挙動の違いについてのメモ

Posted at

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)

無事アニメーションされました

14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12