Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

SwiftUI Animations Tutorial

Introduction

SwiftUI provides a powerful and intuitive way to create animations for your iOS applications. With a few lines of code, you can create smooth, visually appealing transitions and effects. This tutorial will guide you through the various aspects of SwiftUI animations, from basic to advanced concepts.

Basic Animation

Let's start with a simple example of animating a view's opacity. In SwiftUI, you can use the .animation modifier to animate changes to a view's properties.

import SwiftUI

struct ContentView: View {
    @State private var isVisible = true

    var body: some View {
        VStack {
            Button("Toggle Visibility") {
                withAnimation {
                    isVisible.toggle()
                }
            }
            .padding()

            if isVisible {
                Text("Hello, SwiftUI!")
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
                    .opacity(isVisible ? 1 : 0)
            }
        }
    }
}

In this example, tapping the button toggles the visibility of the text with a fade-in and fade-out effect.

Animating Properties

SwiftUI allows you to animate various properties of views, such as scale, rotation, and position. Here is an example of scaling a view:

import SwiftUI

struct ContentView: View {
    @State private var scale: CGFloat = 1.0

    var body: some View {
        VStack {
            Button("Scale Up") {
                withAnimation {
                    scale += 0.5
                }
            }
            .padding()

            Text("Scale Me!")
                .padding()
                .background(Color.green)
                .foregroundColor(.white)
                .cornerRadius(10)
                .scaleEffect(scale)
        }
    }
}

In this example, tapping the button scales the text up by 50% each time.

Custom Animations

You can create custom animations using the Animation struct. For example, you can create a custom animation with a specific duration and easing function:

import SwiftUI

struct ContentView: View {
    @State private var position: CGFloat = 0.0

    var body: some View {
        VStack {
            Button("Move Right") {
                withAnimation(Animation.easeInOut(duration: 2.0)) {
                    position += 100
                }
            }
            .padding()

            Rectangle()
                .fill(Color.red)
                .frame(width: 100, height: 100)
                .offset(x: position, y: 0)
        }
    }
}

In this example, tapping the button moves the rectangle 100 points to the right with a smooth ease-in-out animation over 2 seconds.

Combining Animations

SwiftUI allows you to combine multiple animations to create complex effects. You can chain animations using the .animation modifier:

import SwiftUI

struct ContentView: View {
    @State private var rotate = false
    @State private var scale: CGFloat = 1.0

    var body: some View {
        VStack {
            Button("Animate") {
                withAnimation(Animation.linear(duration: 1.0).repeatForever(autoreverses: false)) {
                    rotate.toggle()
                }
                withAnimation(Animation.easeInOut(duration: 1.0)) {
                    scale = rotate ? 2.0 : 1.0
                }
            }
            .padding()

            Image(systemName: "star.fill")
                .resizable()
                .frame(width: 100, height: 100)
                .rotationEffect(.degrees(rotate ? 360 : 0))
                .scaleEffect(scale)
                .foregroundColor(.yellow)
        }
    }
}

In this example, tapping the button starts a continuous rotation animation and a scaling animation that toggles between 1.0 and 2.0.

Animating Shapes

SwiftUI also allows you to animate shapes. For instance, you can animate the radius of a circle:

import SwiftUI

struct ContentView: View {
    @State private var radius: CGFloat = 50.0

    var body: some View {
        VStack {
            Button("Change Radius") {
                withAnimation {
                    radius = radius == 50.0 ? 100.0 : 50.0
                }
            }
            .padding()

            Circle()
                .frame(width: 200, height: 200)
                .foregroundColor(.orange)
                .cornerRadius(radius)
        }
    }
}

In this example, tapping the button changes the radius of the circle, creating an animation that makes the circle appear to change size.

Conclusion

SwiftUI animations are a powerful tool to make your iOS applications more dynamic and engaging. This tutorial has covered the basics of animating views, properties, and shapes, as well as creating custom and combined animations. Experiment with different animations to find the effects that best suit your application's needs.