iOS

How to Create Star Wars Animated Text Using SwiftUI


Earlier, we published a SwiftUI tutorial showing you how to create perspective text using the framework. It’s very much like Star Wars’ perspective text but without animation. This week, Priyans Brahmbhatt will teach you how to add animations to the perspective text. Since this article was built on the top of the perspective text tutorial, I highly recommend you to check it out first.

Editor’s note: This is a guest post written by Priyans Brahmbhatt.

We will start from where the previous tutorial ended. Let’s have a look at the code again:

How to Create Star Wars Animated Text Using SwiftUI 1

Animating the Text Using SwiftUI

Before writing the code, let’s think about how we can achieve the animation. I guess everyone know how the Star Wars’ text animation looks like. But anyway, here is the animation we want to build:

How to Create Star Wars Animated Text Using SwiftUI 2

What we want to do is to add a text animation which displays text coming from the bottom of the screen. Also, remember the animation should start when view appears.

There are many ways to realize the animation. But one way to achieve it is by tracking the height of the text view. As soon as the view appears, we set the height of the text view to zero and then gradually increase it. At the same time, we ask SwiftUI to handle the animation.

First, the question is how can we define the height for the view?

In SwiftUI, we can define a frame for the view using the .frame modifier and that allows you to modify the width and height properties. Let’s see how it works.

In the Text view, add a frame modifier and give it a width of 300 and height of 750 like this:

You’re free to play around with the numbers. After modifying the code, the preview should show you something like this:

How to Create Star Wars Animated Text Using SwiftUI 3

Next, to keep track of the animation, we need a State property. We will define a property called animationStart and we set its value to false by default. When the view appears, we will toggle it so that view knows that it is a time to start the animation.

Insert the state variable like this:

When the view appears, we need to set it true in order to trigger the animation. To do that, add the onAppear block after the frame modifier:

In the code, we simply toggle the state variable and trigger the animation.

Next, let’s change the height property inside the frame modifier. This is how we animate the height of the frame. When animationStart is set to true, the height of the text view is set to 750. Conversely, the height of the Text View is set to zero. Here is the line of code:

Nothing changes so far. If you run the app by clicking on play button in the preview, the text view is still the same. The reason is that we have not added the animation modifier.

Okay, let’s add animation.

First, define the duration for the animation by declaring another property:

Next, attach the .animation modifier to the text view like this:

Here I am using a linear animation with a duration of 11 seconds. The default duration seems to be ok to present the text but feel free to modify it if you like. SwiftUI comes with many built-in animations. But I found the linear animation works great for this case as we don’t want to vary the animation speed.

Now if you press play, you should see the animation in its full glory. Try it!

swiftui perspective text animation

Looks cool, right?

The animation works but I think we can make it even better. Wouldn’t it be great if we can let users read the text normally by turning the text upright when the animation ends?

How to Create Star Wars Animated Text Using SwiftUI 4

Let’s add another state property named animationEnd to track the status of the animation:

By default, its value is set to false. At some point, we need to toggle it to indicate the animation has ended. Obviously, we want to set the variable to true after 11 seconds, which is the duration of the animation defined earlier. So, update the onAppear block like this:

Here I set the animationEnd property to true after the text animation ends. For the rotation animation that is run when animationedEnd is toggled, I want to set the duration to 1.5 seconds. So, let’s declare another variable for that.

To rotate the text view and make it flat on screen, we need to change the degree parameter in the rotation3DEffect modifier. When animationEnd is set to true, its value should be 0. So, change the code for the rotation3DEffect modifier like this:

If you test the app, the rotation animation already works. But the app still has a problem. The animation duration is still set to 11s instead of 1.5s that we just defined. To fix that, let’s modify the animation modifier like this:

Now if you run the app again, the rotation animation is now much faster and better.

How to Create Star Wars Animated Text Using SwiftUI 5

That’s it! I hope you enjoy this tutorial. As you can see, SwiftUI has made developers’ life easier when dealing with animation. If you have any questions about the tutorial, please leave me comment below.

Editor’s note: This is a guest post written by Priyans Brahmbhatt. Priyans is an independent app developer. You can reach him out on Twitter, Instagram or his own website.

If you want to learn more about SwiftUI, check out our brand new book – Mastering SwiftUI.

iOS
Creating Your Own Custom Controls Using IBDesignable in Xcode 6
iOS
Working With Background Fetch in iOS 7
iOS
A Swift Tutorial for Google Maps SDK
There are currently no comments.

Shares