Calming the Mind: Using Animations in your Swift App

Calming the Mind: Using Animations in your Swift App

Animations have the power to bring an app to life, making it more engaging and interactive for users. They can be used to draw attention to important information, guide users through a process, or simply add spice to an app. If you're a developer working with Swift, you'll be happy to know that it's easy to add animations to your app.

In this article, we'll be exploring how to use animations in a Swift app by building a simple breathing exercise app. We'll start by looking at the code for the app and breaking down how the animations work. From there, we'll discuss some ways to take our app to the next level by adding more visual elements and sound effects. Whether you're a seasoned developer or this is your first Swift project, this article will give you the tools you need to add animations to your own Swift projects.

struct breathingView: View {

    @State private var radius: CGFloat = 50
    @State private var breathIn: Bool = true
    @State private var instruction: String = "Get ready"

    var body: some View {
        ZStack {
            VStack {
                ZStack {
                    Circle()
                        .frame(width: radius * 7, height: radius * 7)
                        .foregroundColor(.blue)
                }
            }
            Text(instruction)
                .font(.title2)
                .fontWeight(.bold)
                .foregroundColor(.white)
                .animation(.easeOut(duration: 0.7))
        }

               .onAppear {
                   Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { _ in
                       withAnimation(Animation.easeInOut(duration: 3)) {
                           self.startBreathing()
                       }
                   }
               }
               .onDisappear {
                   self.instruction = ""
               }
           }

    func startBreathing() {
        switch breathIn {
        case true:
            radius = 75
            instruction = "Breathe in"
            breathIn = false
        case false:
            radius = 50
            instruction = "Breathe out"
            breathIn = true
        }
    }
}

The code for our breathing app includes a struct called breathingView with several @State variables: radius, breathIn, and instruction. The radius variable is used to control the size of the circle, while breathIn is a boolean that keeps track of whether the user is currently breathing in or out. The instruction variable is used to display text on the screen, providing guidance to the user.

The body of our view is a ZStack, which is a container that allows us to stack views on top of each other. In this case, we're using it to display a circle and some text on top of the circle. The circle is created using the Circle shape and the frame modifier, which sets the size of the circle based on the radius variable. The text is simply a Text view with a font and color applied using the font and foregroundColor modifiers.

The real magic happens in the onAppear and onDisappear closures. The onAppear closure is a special SwiftUI function that gets called when the view appears on the screen. We use this opportunity to create a timer that will repeat every five seconds. Inside the timer, we use an animation to call the startBreathing function. This function updates the radius and instruction variables, causing the circle to change size and the text to update.

Creating the animation itself is easy using the withAnimation function and an Animation object. In this case, we're using the Animation.easeInOut function to create an animation that starts and ends slowly, while speeding up in the middle. This creates a smooth, natural-feeling animation that helps guide the user through the breathing exercise. The duration parameter of the Animation.easeInOut function determines how long the animation will take to complete, in seconds.

The startBreathing function itself is relatively simple. It uses a switch statement to check the value of the breathIn variable. If breathIn is true, it means the user is currently breathing in, so we increase the size of the circle and update the instruction variable to say "Breathe in". If breathIn is false, it means the user is currently breathing out, so we decrease the size of the circle and update the instruction variable to say "Breathe out". We also flip the value of the breathIn variable, so that the next time the function is called it will do the opposite action.

The onDisappear closure is another special SwiftUI function that gets called when the view disappears from the screen. In this case, we're using it to reset the instruction variable to an empty string, so that the text is no longer displayed when the view is not visible.

Overall, this code creates a simple but effective breathing exercise app that guides the user through the process with animations and text instructions. By using the withAnimation function and the Animation struct, we were able to create a smooth, natural-feeling animation that enhances the user experience.

So far, we've only scratched the surface of what's possible with animations in Swift. To take our app to the next level, we can add more visual elements such as changing the color of the circle as it grows and shrinks. This can be achieved by using the .foregroundColor modifier and updating the color based on the current state of the radius variable. Sound effects can also be used to further enhance the user experience. To add sound effects, you can use the AVFoundation framework and the AVAudioPlayer class to play sound files when certain events occur in your app.

In this article, we learned how to use animations in a Swift app by building a simple breathing exercise app. We saw how easy it is to create animations using the withAnimation function and the Animation struct, and how these animations can bring our app to life. We also looked at ways to enhance the user experience by adding more visual elements and sound effects.

If you enjoyed this article and want to stay up-to-date on the latest in app development, consider subscribing to my newsletter. You'll get access to exclusive content, early access to new articles, and much more. Thank you for reading! If you have any questions, leave them in the comments below.

Did you find this article valuable?

Support Jake Watembach by becoming a sponsor. Any amount is appreciated!