Skip to main content

How to create basic animations

About 3 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to create basic animations 관련

SwiftUI by Example

Back to Home

How to create basic animations | SwiftUI by Example

How to create basic animations

Updated for Xcode 15

Improved in iOS 17

SwiftUI has built-in support for animations with its animation() modifier. To use this modifier, place it after any other modifiers for your views, tell it what kind of animation you want, and also make sure you attach it to a particular value so the animation triggers only when that specific value changes.

For example, this code creates a button that increases its scale effect by 1 each time it’s pressed:

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

    var body: some View {
        Button("Press here") {
            scale += 1
        }
        .scaleEffect(scale)
        .animation(.linear(duration: 1), value: scale)
    }
}

Download this as an Xcode projectopen in new window

That makes the animation happen over 1 second, but if you don’t want to specify a precise time for your animation you can just use .linear.

Important

From iOS 17 and later SwiftUI uses spring animations by default, but before that used linear animations by default.

Instead of simple linear animations you can specify a curve from various built-in options, including:

  • .easeIn starts slow then accelerates until the end.
  • .easeOut starts fast then slows down near the end.
  • .easeInOut starts slow, accelerates in the middle, then slows down near the end.
  • .smooth is a spring animation with no bounce (from iOS 17)
  • .snappy is a spring animation with a little bounce (from iOS 17)
  • .bouncy is a spring animation with a medium amount of bounce (from iOS 17)

Alternatively, you can specify .timingCurve to specify your own control points.

For example, this animates the scale effect so that it starts slow and gets faster:

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

    var body: some View {
        Button("Press here") {
            scale += 1
        }
        .scaleEffect(scale)
        .animation(.easeIn, value: scale)
    }
}

Download this as an Xcode projectopen in new window

You can animate many other modifiers, such as 2D and 3D rotation, opacity, border, and more. For example, this makes a button that spins around and increases its border every time it’s tapped:

struct ContentView: View {
    @State private var angle = 0.0
    @State private var borderThickness = 1.0

    var body: some View {
        Button("Press here") {
            angle += 45
            borderThickness += 1
        }
        .padding()
        .border(.red, width: borderThickness)
        .rotationEffect(.degrees(angle))
        .animation(.easeIn, value: angle)
    }
}

Download this as an Xcode projectopen in new window

Similar solutions…
How to create multi-step animations using phase animators | SwiftUI by Example

How to create multi-step animations using phase animators
How to synchronize animations from one view to another with matchedGeometryEffect() | SwiftUI by Example

How to synchronize animations from one view to another with matchedGeometryEffect()
How to override animations with transactions | SwiftUI by Example

How to override animations with transactions
How to apply multiple animations to a view | SwiftUI by Example

How to apply multiple animations to a view
How to reduce animations when requested | SwiftUI by Example

How to reduce animations when requested

이찬희 (MarkiiimarK)
Never Stop Learning.