I want to connect animations with Swift UI.

What I want to do

  1. Press Button
  2. The screen rotates (Can be executed with the following code so far)
  3. Change the size of the Rectangle (eg. height from 250 to 100)
import SwiftUI

struct ContentView: View {
  @State private var flag = false
   
  var body: some View {
    VStack {
      Rectangle()
        .fill(.blue)
        .frame(height: 250)
        .rotationEffect(Angle.degrees(flag ? 90 : 0))
        .animation(.easeInOut(duration: 1), value: flag)
         
      Button("toggle") {
        flag.toggle()
      }
    }
  }
}

How can I use SwiftUI for continuous animation such as rotation → resizing ?

Answered by Claude31 in 712334022

Just add a scaleEffect:

struct ContentView: View {
    @State private var flag = false
    @State var amount = 1.0
    
    var body: some View {
        VStack {
            Spacer()
            Rectangle()
                .fill(.blue)
                .frame(width: 50, height: 50)
                .rotationEffect(Angle.degrees(flag ? 90 : 0))
                .scaleEffect(amount)
                .animation(.easeInOut(duration: 1), value: flag)
            Spacer()
            Button("toggle") {
                flag.toggle()
                amount = amount == 1 ? 2 : 1 // toggle effect
            }
        }
    }
}
Accepted Answer

Just add a scaleEffect:

struct ContentView: View {
    @State private var flag = false
    @State var amount = 1.0
    
    var body: some View {
        VStack {
            Spacer()
            Rectangle()
                .fill(.blue)
                .frame(width: 50, height: 50)
                .rotationEffect(Angle.degrees(flag ? 90 : 0))
                .scaleEffect(amount)
                .animation(.easeInOut(duration: 1), value: flag)
            Spacer()
            Button("toggle") {
                flag.toggle()
                amount = amount == 1 ? 2 : 1 // toggle effect
            }
        }
    }
}

Thank you. I have the implementation I envisioned. Happiness to you.

I want to connect animations with Swift UI.
 
 
Q