如何在 SwiftUI 中通过函数调用直接切换视图?

How can I switch the View directly with a function call in SwiftUI?

嘿,我是 Swift 和 SwiftUI 的新手,我想知道如何在视图之间切换

正如您在下面的代码中看到的,我制作了一个闪屏动画,我希望我的应用程序在状态“endSplash”为真时切换到下一个视图,但我不知道如何实现这个目标.我已经阅读了如何使用 NavigationView 和 NavigationLink 更改视图,但问题是我不希望用户按下 button/text 我想直接切换视图。有没有我可以调用的函数来直接更改视图(比如在 Android Studio 中我可以开始一个新的 Intent)?

struct ContentView: View {
    @State var animate = false
    @State var endSplash = false
    var body: some View {
        NavigationView{
        ZStack{
            Color("Primary")
            
            Image("LBig").resizable().renderingMode(.original).aspectRatio(contentMode: animate ? .fill : .fit)
                .frame(width: animate ? nil : 85, height: animate ? nil: 85)
                .colorInvert().scaleEffect(animate ? 3 : 1).frame(width: UIScreen.main.bounds.width)
        }.ignoresSafeArea(.all, edges: .all).onAppear(perform: {
            animateSplash()
        }).opacity(endSplash ? 0:1)
    }
}
    func animateSplash(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
            withAnimation(Animation.easeOut(duration: 0.45)){
                animate.toggle()
            }
            withAnimation(Animation.linear(duration: 0.35)){
                endSplash.toggle()
                //Switch to another View here I guess ?
            }
        }
    }
}

既然你已经有了一个 Bool 你所需要的只是一个条件。

如果您想在导航堆栈中使用它(带有后退按钮),请将 NavigationLink 构造函数与 isActive 一起使用,并使用您的 Bool 激活 View .

import SwiftUI

struct SwitchScreen: View {
    @State var animate = false
    @State var endSplash = false
    var body: some View {
        NavigationView{
            switch endSplash{
            case false:
                ZStack{
                    
                    Color.blue
                    
                    Image(systemName: "checkmark").resizable().renderingMode(.original).aspectRatio(contentMode: animate ? .fill : .fit)
                        .frame(width: animate ? nil : 85, height: animate ? nil: 85)
                        .colorInvert().scaleEffect(animate ? 3 : 1).frame(width: UIScreen.main.bounds.width)
                    
                    
                }.ignoresSafeArea(.all, edges: .all).onAppear(perform: {
                    animateSplash()
                }).opacity(endSplash ? 0:1)
            case true:
                Text("Done")
            }
        }
    }
    func animateSplash(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
            withAnimation(Animation.easeOut(duration: 0.45)){
                animate.toggle()
            }
            withAnimation(Animation.linear(duration: 0.35)){
                endSplash.toggle()
                //Switch to another View here I guess ?
            }
        }
    }
}
struct SwitchScreen_Previews: PreviewProvider {
    static var previews: some View {
        SwitchScreen()
    }
}

我已经对您的代码进行了一些修改,希望对您有所帮助。

struct ContentView: View {
    @State var animate = false
    @State var endSplash = false
    var body: some View {
        NavigationView {
            ZStack{
                NavigationLink(
                    destination: YourDestinationView(),
                    isActive: $endSplash
                ) { EmptyView() }
                Color("Primary")
                
                Image("LBig").resizable().renderingMode(.original).aspectRatio(contentMode: animate ? .fill : .fit)
                    .frame(width: animate ? nil : 85, height: animate ? nil: 85)
                    .colorInvert().scaleEffect(animate ? 3 : 1).frame(width: UIScreen.main.bounds.width)
            }.ignoresSafeArea(.all, edges: .all).onAppear(perform: {
                animateSplash()
            }).opacity(endSplash ? 0:1)
        }
    }
    func animateSplash(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5){
            withAnimation(Animation.easeOut(duration: 0.45)){
                animate.toggle()
            }
            withAnimation(Animation.linear(duration: 0.35)){
                endSplash.toggle()
                //Switch to another View here I guess ?
            }
        }
    }
}