What I want to do
- Press Button
- The screen rotates (Can be executed with the following code so far)
- 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
?
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
}
}
}
}