从 SpriteKit 场景移动到 SwiftUI 视图

Move From SpriteKit scene to SwiftUI View

我正在尝试找出从 SpriteKit 场景返回 SwiftUI 视图的正确方法。我目前有一个 Swift UI“主菜单”,看起来像这样。

struct MainMenu: View {
    var body: some View {
        NavigationView {
            VStack {
                
                Text("Replicator")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                    .padding()
                
                NavigationLink(destination: ContentView().navigationBarBackButtonHidden(true)) {
                    HStack {
                        Image(systemName: "play.circle")
                            .font(.title3)
                        
                        Text("Start")
                            .font(.title)
                        
                    }
                    .frame(width: 250, height: 50)
                    .background(.cyan)
                    .cornerRadius(25)
                    .foregroundColor(.white)
                }
            }
        }
    }
}

ContentView() 包含 SpriteKit 游戏,如下所示。

struct ContentView: View {
    
    var scene: SKScene {
        
        let scene = Level_1()
        scene.size = CGSize(width: 750, height: 1334)
        scene.scaleMode = .aspectFit
        return scene
        
    }
    
    var body: some View {
        
        
        
        VStack {
            
            SpriteView(scene: scene)
                .ignoresSafeArea()
            
           
            
        }
    }
}

我的问题是...进入 ContentView 后如何return 到“主菜单”?

感谢您提供的任何帮助。

您可以使用

@Environment(.presentationMode) var presentationMode

所以你可以创建一个按钮并调用

presentationMode.wrappedValue.dismiss()

或者您可以将绑定变量传递给内容视图并将其设置为 false,如下所示:

在主菜单中

struct MainMenu: View {
    @State var isPresented = false
    var body: some View {
        NavigationView {
            VStack {             
                Text("Replicator")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                    .padding()
                NavigationLink(destination: ContentView(isPresented: $isPresented).navigationBarBackButtonHidden(true), isActive: $isPresented) {
                    HStack {
                        Image(systemName: "play.circle")
                            .font(.title3)
                        Text("Start")
                            .font(.title)
                    
                    }
                    .frame(width: 250, height: 50)
                    .background(.cyan)
                    .cornerRadius(25)
                    .foregroundColor(.white)
                }
            }
        }
    }
}

在内容视图中:

struct ContentView: View {
    @Binding var isPresented: Bool
    var scene: SKScene {
    let scene = Level_1()
        scene.size = CGSize(width: 750, height: 1334)
        scene.scaleMode = .aspectFit
        return scene
    }
    var body: some View {
        ZStack {            
            SpriteView(scene: scene)
                .ignoresSafeArea() 
            Button(action: { //You can put the button wherever you want as long as you pass in the isPresented Binding
                isPresented.toggle()
            }) {
                Text("Back to MainMenu")
            }
        }
    }
}