0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Swift] オブジェクトの回転の中心をずらす

Last updated at Posted at 2022-05-31

iPhoneアプリを開発している時にオブジェクトを回転させるアニメーションを付けたいことはよくあると思います。

今回私はオブジェクトの左端を回転の中心にしようと思ったらハマってしまったので、記録として残しておきます。

オブジェクトの回転

オブジェクトを回転させる方法として今回は、CGAffineTransformを使います。

例として、UIViewを回転させることを考えます。
画面中央に配置したUIViewを時計回りに60度回転させるならば

view.transform = CGAffineTransform(rotationAngle: .pi / 3)

と書けば下の画像のようになります。

Simulator Screen Shot - iPod touch (7th generation) - 2022-06-01 at 07.34.59.png

この場合回転の中心はUIViewの中心になっています。

回転の中心をずらす

回転の中心をずらすのはとても簡単です。
オブジェクトの左上を回転の中心とする場合は

view.layer.anchorPoint = CGPoint(x: 0.0, y: 0.0)

としてあげればうまくいきます。
他にも、

view.layer.anchorPoint = CGPoint(x: 1.0, y: 0.0) //右上
view.layer.anchorPoint = CGPoint(x: 1.0, y: 1.0) //右下
view.layer.anchorPoint = CGPoint(x: 0.0, y: 1.0) //左下

最後に

回転の中心をずらすことによって、
時計の針やレーダーのソナーみたいなものも再現できるようになると思います。

最後までありがとうございました。何かあればコメントしてください。

参考リンク

CGAffineTransform

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?