LoginSignup
1
0

More than 5 years have passed since last update.

CABasicAnimationでUIViewが回転などアニメーションしない件

Posted at

viewDidLoadでアニメーションのコードを書いても回転しないのでハマっていたのですが、単純にViewWillAppear以降じゃないとアニメーションしないというオチでした。意外に言及されてないようなのでメモしておきます。

UIViewのアニメーションについては、「UIView 回転」で検索すれば山ほど出てくるので、そちらをご参考に下さい。と、いいつつ一応書いておきます。永遠にくるくる回転するサンプルコードです。

let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
// 起点を0度とする
rotateAnimation.fromValue = 0.0
// 終点を360度とする
rotateAnimation.toValue = CGFloat(.pi * 2.0)
// 2秒で1回転
rotateAnimation.duration = 2.0
// 永遠に回転
rotateAnimation.repeatCount = .infinity
// アニメーションを追加
imageView.layer.add(rotateAnimation, forKey: nil)
1
0
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
1
0