SwiftUI 链接动画

SwiftUI Chaning Animations

我正在尝试在 SwiftUI 中链接两个动画。但是,第一个动画在按下按钮时没有动画。我在这里找到了这种链接动画的方法: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
                }
            }

        }
    }
}

您可以稍后使用 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
                    }
                }
            }

        }
    }
}