NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

How to create basic animations

Paul Hudson    @twostraws   

Updated for Xcode 12.5

SwiftUI has built-in support for animations with its animation() modifier. To use this modifier, place it after any other modifiers for your views, and tell it what kind of animation you want.

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: CGFloat = 1

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

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.

Instead of simple linear animations, you can specify a curve from .easeIn, .easeOut, .easeInOut, or use .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: CGFloat = 1

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

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: Double = 0
    @State private var borderThickness: CGFloat = 1

    var body: some View {
        Button("Press here") {
            angle += 45
            borderThickness += 1
        }
        .padding()
        .border(Color.red, width: borderThickness)
        .rotationEffect(.degrees(angle))
        .animation(.easeIn)
    }
}
Hacking with Swift is sponsored by ViRE

SPONSORED ViRE offers discoverable way of working with regex. It provides really readable regex experience, code complete & cheat sheet, unit tests, powerful replace system, step-by-step search & replace, regex visual scheme, regex history & playground. ViRE is available on Mac & iPad.

Download on the App Store

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.9/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.