Home Guides onAppear

A SwiftUI onAppear animation

A SwiftUI onAppear animation runs when the view shows up, and then it is finished. The first frame is the start. withAnimation carries it to the rest.

A sketch of the motion. The file below plays live in Swoop, and the same source pastes into Xcode.

The file

import SwiftUI

struct RiseIn: View {
    @State private var shown = false

    var body: some View {
        Circle()
            .fill(.cyan)
            .frame(width: 64, height: 64)
            .offset(y: shown ? 0 : 28)
            .opacity(shown ? 1 : 0)
            .onAppear {
                withAnimation(.easeOut(duration: 0.45)) {
                    shown = true
                }
            }
    }
}

shown starts false, so the circle is 28 points down and fully transparent. onAppear sets it true inside withAnimation(.easeOut(duration: 0.45)). Offset and opacity both read shown, so both ease. Ease-out spends the speed at the beginning and arrives gently.

Without withAnimation, the assignment still happens, and the circle still ends in the right place. It just appears there. The wrapper is the difference between a cut and a move.

Once, not forever

onAppear does not repeat. A loader that has to keep going wants a TimelineView. A move the person starts wants a button. This file is the arrival.

Keep going

Download Swoop for MacLifetime Pro

Free. macOS 14 or later, Apple silicon or Intel. The file plays live in Swoop.