按钮不更改SwiftUI中的视图

按钮不更改SwiftUI中的视图,swiftui,swiftui-navigationlink,Swiftui,Swiftui Navigationlink,因此,在我的ContentView.swift中,我有以下代码: import SwiftUI extension UIScreen{ static let screenWidth = UIScreen.main.bounds.size.width static let screenHeight = UIScreen.main.bounds.size.height static let screenSize = UIScreen.main.bounds.size } str

因此,在我的ContentView.swift中,我有以下代码:

import SwiftUI

extension UIScreen{
   static let screenWidth = UIScreen.main.bounds.size.width
   static let screenHeight = UIScreen.main.bounds.size.height
   static let screenSize = UIScreen.main.bounds.size
}

struct ContentView: View {
    @State var Direction: Int = 0
    @State var ButtonsShowing: Bool = true
    @State var View: Int = 0

    func MoveLeft() {
        if ButtonsShowing == true {
            ButtonsShowing = false
        }
    }

    func MoveRight() {
        if ButtonsShowing == true {
            ButtonsShowing = false
        }
        Direction = 1
    }

    var body: some View {
        ZStack {
            Image("Space").resizable()
                .frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight + 50)
                .edgesIgnoringSafeArea(.all)

            VStack {
                HStack {
                    Button(action: MoveLeft) {
                        if ButtonsShowing == true {
                            NavigationLink(destination: Playing()) {
                                Image("LeftClick")
                                    .frame(width: UIScreen.screenWidth / 2, height: UIScreen.screenHeight)
                            }

                        }
                    }

                    Spacer()

                    Button(action: MoveRight) {
                        if ButtonsShowing == true {
                            Image("RightClick")
                                .frame(width: UIScreen.screenWidth / 2, height: UIScreen.screenHeight)
                        }
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
我可以预览一下:

如果你看我的左键点击按钮,我想它带我到一个不同的视图,但不知道如何。我在其中有一个NavigationLink,我的目的地是不同的视图(Playing.swift,如果需要知道,我使用Playing()调用它)

显然,我做错了什么,我[[希望]]肯定地知道这是从按钮,而不是从另一个角度:

NavigationLink(destination: Playing()) {
    Image("LeftClick")
        .frame(width: UIScreen.screenWidth / 2, height: UIScreen.screenHeight)
}

提前感谢。

在ZStack之前添加NavigationView,没有NavigationView,NavigationLink将无法工作

var body: some View {
  NavigationView {
    ZStack {
    ...
    }
  }
}