简体   繁体   English

SwiftUI animation 带switch语句

[英]SwiftUI animation with switch statement

I understand how simple animations work in SwiftUI.我了解简单动画在 SwiftUI 中的工作原理。 However, I have a slightly more complex watchOS application.但是,我有一个稍微复杂的 watchOS 应用程序。 View state is handled by a switch statement:查看 state 由switch语句处理:

struct ContentView: View {
    @EnvironmentObject private var state: AppState

    @State private var selection = 1

    var body: some View {
        Group {
            switch state.view {
            case .start:
                TabView(selection: $selection) {
                    ActivityView()
                        .tag(0)
                    StartView()
                        .tag(1)
                    SettingsView()
                        .tag(2)
                }
            case .workout:
                TabView(selection: $selection) {
                    TakeoffControlView()
                        .tag(0)
                    TakeoffView()
                        .tag(1)
                }
            }

            // ...
        }
    }
}

In another view I edit the view state:在另一个视图中,我编辑视图 state:

struct StartView: View {
    @EnvironmentObject private var state: AppState

    var body: some View {
        Button(action: {
            state.view = .workout
        }, label: {
            Text("Start")
        })
    }
}

How can I add animations to transition between the different cases?如何添加动画以在不同情况之间转换? I tried adding an animation to the Group , TabView and individual views without success.我尝试将 animation 添加到GroupTabView和单个视图但没有成功。 Obviously I wrapped the state change in withAnimation .显然,我将 state 更改包装在withAnimation中。 However, I wasn't able to make it work.但是,我无法使其工作。 Any ideas how to make this work?任何想法如何使这项工作?

.transition() is just defining what the transition is, and to get the expected transition animation, you have to do the changes to SourceOfTruth inside withAnimation() {... } block. .transition()只是定义过渡是什么,并且要获得预期的过渡 animation,您必须在withAnimation() {... }块中对SourceOfTruth进行更改。

struct ContentView: View {
    @EnvironmentObject private var state: AppState

    @State private var selection = 1

    var body: some View {
        Group {
            switch state.view {
            case .start:
                TabView(selection: $selection) {
                    ActivityView()
                        .tag(0)
                    StartView()
                        .tag(1)
                    SettingsView()
                        .tag(2)
                }
                .transition(.slide)//<- here
                
            case .workout:
                TabView(selection: $selection) {
                    TakeoffControlView()
                        .tag(0)
                    TakeoffView()
                        .tag(1)
                }
                .transition(.slide)//<- here
            }

            // ...
        }
    }
}

struct StartView: View {
    @EnvironmentObject private var state: AppState

    var body: some View {
        Button(action: {
            withAnimation(.easeIn) {  //<- here
                state.view = .workout
            }
        }, label: {
            Text("Start")
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM