Skip to content

SwiftUI: Scaling Animation

Animation on SwiftUI is a lot easier to achieve than UIKit with so much more support provided by Apple. I wonder if this will be a good thing or bad thing but regardless, we need to make good use of what is provided right.

In this tutorial, you’ll learn how to perform a scaling animation in SwiftUI.

Prerequisites

To follow along this tutorial, you’ll need some basic knowledge in:

  • A basic familiarity with Swift.
  • At least Xcode 11

Setting up the view

You will be given a button where you will animate when the button is clicked. The first thing to do is to create a variable know if the button is being clicked or not.

Then you will create another variable which represent Animation so that it is easy to tweak from here.

You can play around with the animation in a bit if you create your variable as above. You will create a button and a Rectangle that will simulate the animation.

  1. When you click on the button, you will toggle the bool value.
  2. A rectangle is created with its properties defined.

Scaling Effect

You may modify the animation effect from here by replacing the animation.

Let’s explore multiple different animation. The first would be easeOut where it will slow down at the end of the animation.

The next animation is easeIn where it will start slow then it will speed up.

The next animation is linear where it will maintain a constant speed throughout the duration of the animation.

The last one is easeInOut where it will start slow then speed up and finally slow down at the end.

Non Stop Animation

Requirement changes, now you are required to rotate the rectangle non stop. I know it looks like a Square, I should have given it a bigger width. Anyway, let’s tweak the animation a little bit.

You will need to tweak 2 line of code to make this to work. With repeatForever, it will animate non stop and by including false, it will not reset but rotate continuously.

And the effect is as shown:

Anchor Point

You can even scale to your desired anchor point. Currently the default anchor point is center. Let’s try bottomLeading and it should scale to the left bottom.

You can even try many different anchor point such as leading, trailing, top, bottom, topLeading, topTrailing, bottomLeading and bottomTrailing. Have fun exploring.

SwiftUI has definitely bring the animation to a whole new level. I love SwiftUI! Check out more post that I posted about SwiftUI at http://daddycoding.com/swiftui/

Published inSwiftUI

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *