๐Ÿ”ฐ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 value (view state) to monitor for changes.

struct ContentView: View {

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

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

Last updated