0

使用 SwiftUI 和我已扩充的滑块/侧边菜单教程,以便对每个侧边菜单选择进行操作。

当显示侧边菜单并点击菜单选项时,它效果很好,并带我进入带有菜单项的新视图。但是当我点击并看到侧面菜单仍然存在时,所有菜单项都没有死。菜单项仍然会为单击设置动画(带有闪烁),但没有任何反应。我必须关闭侧面菜单,重新打开它,然后菜单项再次工作 - 一次。

谁能告诉我为什么会这样?

这是漂亮的内容视图、主视图和侧面菜单视图。

//ContentView.swift

import SwiftUI

struct ContentView: View {

    @State var showMenu = false

var body: some View {

    let drag = DragGesture()
        .onEnded {
            if $0.translation.width < -100 {
                withAnimation {
                    self.showMenu = false
                }
            }
        }

    return NavigationView {
        GeometryReader { geometry in
            ZStack(alignment: .leading) {
                MainView(showMenu: self.$showMenu)
                    .frame(width: geometry.size.width, height: geometry.size.height)
                    .offset(x: self.showMenu ? geometry.size.width/2 : 0)
                    .disabled(self.showMenu ? true : false)
                if self.showMenu {
                    MenuView()
                        .frame(width: geometry.size.width/2)
                        .transition(.move(edge: .leading))
                }
            }
                .gesture(drag)
        }
            .navigationBarTitle("Side Menu", displayMode: .inline)
            .navigationBarItems(leading: (


                   Button(action: {
                        withAnimation {
                            self.showMenu.toggle()
                        }
                    }) {
                        Image(systemName: "line.horizontal.3")
                            .imageScale(.large)
                    }
                ))
        }
    }
}

struct MainView: View {

    @Binding var showMenu: Bool

    var body: some View {
        Button(action: {
            withAnimation {
               self.showMenu = true
            }
        }) {
            Text("Show Menu")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

这是侧面菜​​单视图。

//MenuView.swift

import SwiftUI


struct PlayerView: View {
    @State var showMenu = true
    //@EnvironmentObject var session: SessionStore

    var body: some View {
        VStack{
            //self.showMenu = true
            Text("Manage Players Here").foregroundColor(.red)

         }
    }
}


struct MenuView: View {
    @State var showMenu = true


    var body: some View {

        VStack(alignment: .leading) {

            HStack() {

                NavigationLink(destination: PlayerView()) {
                    HStack(){
                        Image(systemName: "person")
                           .foregroundColor(.gray)
                           .imageScale(.large)
                        Text("Players")
                        .foregroundColor(.gray)
                        .font(.headline)
                    }
                }
            }
            .padding(.top, 100)

            }
            .padding()
            .frame(maxWidth: .infinity, alignment: .leading)
            .background(Color(red: 32/255, green: 32/255, blue: 32/255))
            .edgesIgnoringSafeArea(.all)
    }
}


struct MenuView_Previews: PreviewProvider {
    static var previews: some View {
        MenuView()
    }
}

    enter code here
4

1 回答 1

2

1) 在 MenuView 中绑定 var

2) OnAppear{} 用 Zstack 关闭showMenu

      struct ContentView: View {
                                                  @State var showMenu = false

                                                var body: some View {

                                                    let drag = DragGesture()
                                                        .onEnded {
                                                            if $0.translation.width < -100 {
                                                                withAnimation {
                                                                    self.showMenu = false
                                                                }
                                                            }
                                                        }

                                                    return NavigationView {
                                                        GeometryReader { geometry in
                                                            ZStack(alignment: .leading) {
                                                                MainView(showMenu: self.$showMenu)
                                                                    .frame(width: geometry.size.width, height: geometry.size.height)
                                                                    .offset(x: self.showMenu ? geometry.size.width/2 : 0)
                                                                    .disabled(self.showMenu ? true : false)
                                                                if self.showMenu {
                                                                     MenuView(showMenu: self.$showMenu)
                                                                        .frame(width: geometry.size.width/2)
                                                                        .transition(.move(edge: .leading))
                                                                }
                                                            }
                                                                .gesture(drag).onAppear {
                                                                    self.showMenu = false
                                                                }
                                                        }
                                                            .navigationBarTitle("Side Menu", displayMode: .inline)
                                                            .navigationBarItems(leading: (


                                                                   Button(action: {
                                                                        withAnimation {
                                                                            self.showMenu.toggle()
                                                                        }
                                                                    }) {
                                                                        Image(systemName: "line.horizontal.3")
                                                                            .imageScale(.large)
                                                                    }
                                                                ))
                                                        }
                                                    }
                                                }

                                                struct MainView: View {

                                                    @Binding var showMenu: Bool

                                                    var body: some View {
                                                        Button(action: {
                                                            withAnimation {
                                                               self.showMenu = true
                                                            }
                                                        }) {
                                                            Text("Show Menu")
                                                        }
                                                    }
                                                }




                                struct PlayerView: View {
                                    @State var showMenu = true
                                    //@EnvironmentObject var session: SessionStore

                                    var body: some View {
                                        VStack{
                                            //self.showMenu = true
                                            Text("Manage Players Here").foregroundColor(.red)

                                         }
                                    }
                                }


                                struct MenuView: View {
                                    @Binding var showMenu: Bool // = true


                                    var body: some View {

                                        VStack(alignment: .leading) {

                                            HStack() {

                                                NavigationLink(destination: PlayerView()) {
                                                    HStack(){
                                                        Image(systemName: "person")
                                                           .foregroundColor(.gray)
                                                           .imageScale(.large)
                                                        Text("Players")
                                                        .foregroundColor(.gray)
                                                        .font(.headline)
                                                    }
                                                }
                                            }
                                            .padding(.top, 100)

                                            }
                                            .padding()
                                            .frame(maxWidth: .infinity, alignment: .leading)
                                            .background(Color(red: 32/255, green: 32/255, blue: 32/255))
                                            .edgesIgnoringSafeArea(.all)

                                    }
                                }
于 2020-01-04T04:13:07.540 回答