Today you shall explore a very basic swift snippet in which you will learn how to use .transition in SwiftUI and Xcode 11

Objective: The goal is to have the user tap a button and have some text slide in, and vice versa.

My name is Maxwell, and I run maxcodes.io. Nice to finally meet you.

Step 1 — create a new SwiftUI application.

Make sure SwiftUI is selected

Step 2 — Add a VStack with a labeled Button.

We don’t want the default Text, we want a VStack and a button we can use to toggle our state.

Leave action blank, for now.

Add a Text view for our button’s label

You should now have a labeled button on your screen when you compile.

Step 3 — State, let’s toggle it.

In order to keep track of whether or not the text needs to slide in or out, we need to create a state variable. Once we have created that we need to toggle it, and then finally we need to animate it.

Create a state variable called active

Toggle active whenever the button is clicked

Wrap the toggle action with withAnimation

Step 4 — Transition in the desired content

Now, all we need to do is check active for a true value, and then apply a transition to whatever view we slide in.

Check if Active is true

If Active is true, add a Text view to/within the VStack

Apply a transition for a cool effect

Step 5— That’s all folks!

The new Text should slide in and out whenever you tap the button

If you want to learn more about SwiftUI and learn much more about SwiftUI such as Drawing Paths and Shapes in SwiftUI, see the Complete SwiftUI Development course with this discounted coupon.

Tweet me your article and video requests and I will make them!

Thanks Fam!

I truly appreciate those of you who watch my videos, read my articles, and take my courses.

Thanks,

And as always, I will see you in the very next video.

-Max.