View Animation Rotation in Swift

Tram Ho

Introduce

Hello friends ! In this article, I will guide you to make a simple 2D solar system using Animation in Swift. The images I used in this project can be downloaded here. https://drive.google.com/drive/u/1/folders/1Ms1S_OSUqX3lsf5ElCjMc0AexiRhhw9Y

Begin

First we will create a UIImageView in the middle of our screen. This UIImageView we will image the sun.!

Next, we will create a container view outside with the width and height equal to the UIImageView. This Container View will contain images of our planets, so inside this we will create a UIImage View in it.

We will create more container views corresponding to the number of planets that we want to display in our application. Here I just give an example of three planets so I only create three container views.

Animation settings

Next in order for these planets to move around the sun we need to add animations to it.

Here, we will adjust the top contant of the planets so that it is a little bit away from our sun.

We create a function to initiate the animation in the UIView extension.

This animation will tranform in pi so that the container view containing our planet can be rotated around the sun

Finally, we just need to install each animation for each container view of each planet. The setting for the time duration of the container views is different.

That’s it, we can create a simple solar system using animation in swift.

Thank you for viewing the article.

Share the news now

Source : Viblo