In SwiftUI, you can use the animation modifier to animate changes to view properties such as position, size, rotation, scale, and opacity. Here’s how you can achieve this:

import SwiftUI

struct ContentView: View {
    @State private var isExpanded = false

    var body: some View {
        VStack {
            Rectangle()
                .foregroundColor(.blue)
                .frame(width: isExpanded ? 200 : 100, height: isExpanded ? 200 : 100)
                .rotationEffect(.degrees(isExpanded ? 45 : 0))
                .scaleEffect(isExpanded ? 2 : 1)
                .opacity(isExpanded ? 0.5 : 1)
                .animation(.easeInOut(duration: 1.0)) // Specify animation duration and curve
            
            Button(action: {
                withAnimation {
                    self.isExpanded.toggle()
                }
            }) {
                Text("Toggle Animation")
                    .padding()
                    .foregroundColor(.white)
                    .background(Color.blue)
                    .cornerRadius(8)
            }
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

In this example:

  • We have a ContentView that contains a rectangle and a button.
  • The rectangle’s properties such as size, rotation, scale, and opacity are modified based on the value of the isExpanded state variable.
  • When the button is clicked, the isExpanded state is toggled with animation, causing the rectangle to animate its properties smoothly over time.
  • The .animation modifier applies the animation to all animatable view properties within the same animation block.
  • We use the .easeInOut animation curve with a duration of 1.0 second, but you can choose different animation curves and durations according to your preference.

This code demonstrates how to animate changes to view properties over time in SwiftUI, achieving effects such as resizing, rotating, scaling, and changing opacity. Experiment with different properties and animation modifiers to create the desired visual effects in your SwiftUI app.