How do you animate the size of a view, such that the view may grow or shrink using the frame height? I need to transition between two known dimensions.
I don't know exactly what you need but here is a very basic example with a Rectangle
that gets scaled when you tap the Button
:
struct ContentView: View {
@State var animate = false
var body: some View {
VStack {
Button(action: {
withAnimation {
self.animate.toggle()
}
}, label: {
Text("Animate")
})
Rectangle()
.foregroundColor(.blue)
.frame(width: self.animate ? 100 : 150, height: self.animate ? 60 : 90)
}
}
}
Please add some code to your next question or edit the question so people can provide a more specific answer.
Since withAnimation { }
animate everything related to State changes inside the closure , we can use .animation()
modifier to animate specific one.
struct ContentView: View {
@State var animate = false
var body: some View {
VStack {
Button(action: {
self.animate.toggle()
}, label: {
Text("Animate")
})
Rectangle()
.foregroundColor(.blue)
.frame(width: animate ? 100 : 150, height: animate ? 60 : 90)
.animation(.default) //you can change the animation you need
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With