๐Ÿ”ฐanimate state changes

SwiftUI โŸฉ view โŸฉ state โŸฉ animate

Attach a .animation(_:value:) modifier to the view you want to animate, and select an Animation as well as a state value to monitor for changes.

struct ContentView: View {

    // state value
    @State var isOn = false
    
    // view UI
    var body: some View {
        VStack {
            Circle()
                // view modifiers (change appearances)
                .frame(maxHeight: 200)
                .foregroundColor(isOn ? .red : .yellow)
                .scaleEffect(isOn ? 1.0 : 0.7)
                
                // โญ๏ธ view animation 
                .animation(.default, value: isOn)

            Button("Press Me") { isOn.toggle() }
        }
    }
}

Last updated