我正在尝试在SwiftUI中链接两个动画。但是,当按下按钮时,第一个动画不会设置动画。我在这里找到了链接动画的方法:在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
}
}
}
}
}
您可以在一段时间后使用DispatchQueue和async来修复此问题。因此,从动画中删除延迟并将其传递给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
}
}
}
}
}
}