LoginSignup
4
1

More than 5 years have passed since last update.

【Swift】ConstraintでAnimationをするときに絶対に気をつけたほうがいいこと

Posted at

まぁ、普通に以下のようにAnimationのコードを書いていたりすると思うのですが、以下のようにしていたらハマりました。

hoge.swift
self.hogeButtonBottomConstraint.constant = 8
UIView.animate(withDuration: 0.2) {
  self.view.layoutIfNeeded()
}

何がまずいかっていうと、self.view.layoutIfNeeded()をAnimationさせる前に叩いていないので、以下の処理が呼ばれる前にConstraintを設定すると全部Animationされてしまうんですよね。
なので、以下のようにAnimaitonしたいConstraintの修正のみをself.view.layoutIfNeeded()ではさんであげる必要があります。

hoge.swift
// ここに以下のような処理があると一緒にAnimationされちゃう
// self.fugaButtonBottomConstraint.constant = 8

self.view.layoutIfNeeded()
self.hogeButtonBottomConstraint.constant = 8
UIView.animate(withDuration: 0.2) {
  self.view.layoutIfNeeded()
}

まぁ、皆さんはご存知だとは思うんですけどね...

4
1
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
4
1