I am trying to chain two animations in SwiftUI. However, the first animation does not animate when pressing the button. I found this approach of chaining animations here: Chaining animations in SwiftUI
struct FancyButtonViewModel: View {
@State var movementY:CGFloat = 0
var body: some View {
VStack{
Text("😇")
.offset(y: movementY)
Button("Press Me"){
withAnimation(Animation.easeOut(duration: 0.5)) {
movementY = -150
}
withAnimation(Animation.easeIn(duration: 3).delay(0.5)) {
movementY = 0
}
}
}
}
}
You can fix this by using DispatchQueue and async after a while. So remove the delay from the animation and pass it to the DispatchQueue.
struct ContentView: View {
@State var movementY: CGFloat = 0
var body: some View {
VStack{
Text("😇")
.offset(y: movementY)
Button("Press Me"){
withAnimation(Animation.easeOut(duration: 0.5)) {
movementY = -150
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
withAnimation(Animation.easeIn(duration: 3)) {
movementY = 0
}
}
}
}
}
}
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