2

我正在尝试在 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
                }
            }

        }
    }
}
4

1 回答 1

2

您可以在一段时间后使用 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
                    }
                }
            }

        }
    }
}
于 2020-11-22T01:51:59.480 回答