【问题标题】:SwiftUI multiple Images tap gesture how to do fullScreenCover on image clickSwiftUI多个图像点击手势如何在图像点击时进行fullScreenCover
【发布时间】:2021-03-02 07:48:39
【问题描述】:

我有 2 张图像具有 onTapGesture 。当我点击一张图片时,我可以看到只有点击过的图片被点击,但是我的问题是 fullScreenCover 。如果我单击任何图像,它始终默认为 PersonMain 图像视图,然后转到 InboxView() 。我尝试将 fullScreenCover 放在 onTapGesture 中,但这不起作用。任何建议都会很棒,因为我是 SwiftUI 的新手。我还将 isOpen 状态设置为 false 并在 Tap 时将其设置为 true 但是第一个 Image View InBoxView() 始终是弹出的。

struct MainView: View {
    @State var isOpen = true
    var body: some View {
        
        VStack(spacing: 7.0) {
            HStack(spacing: 7.0) {
                Image("PersonMain")
                    .padding(.leading, 30.0)
                    .buttonStyle(BorderlessButtonStyle())
                    .frame(width:20.0, height: 25.0)
                    .onTapGesture {
                        print("Profile")
                    }.fullScreenCover(isPresented: $isOpen,content: {
                        InboxView()
                   })
                
                Image("RankingMain")
                    .padding(.leading, 70.0)
                    .buttonStyle(BorderlessButtonStyle())
                    .frame(width: 20.0, height: 25.0)
                    .onTapGesture {
                        print("Ranking")
                    }.fullScreenCover(isPresented: $isOpen,content: {
                        SearchView()
                   })
         }
        }
      }
   }

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    它需要为每个fullScreenCover使用单独的状态:

    struct MainView: View {
        @State private var isOpen1 = false
        @State private var isOpen2 = false
    
        var body: some View {
            
            VStack(spacing: 7.0) {
                HStack(spacing: 7.0) {
                    Image("PersonMain")
                        .padding(.leading, 30.0)
                        .buttonStyle(BorderlessButtonStyle())
                        .frame(width:20.0, height: 25.0)
                        .onTapGesture {
                            print("Profile")
                            self.isOpen1.toggle()
                        }.fullScreenCover(isPresented: $isOpen1, content: {
                            InboxView()
                       })
                    
                    Image("RankingMain")
                        .padding(.leading, 70.0)
                        .buttonStyle(BorderlessButtonStyle())
                        .frame(width: 20.0, height: 25.0)
                        .onTapGesture {
                            print("Ranking")
                            self.isOpen2.toggle()
                        }.fullScreenCover(isPresented: $isOpen2, content: {
                            SearchView()
                       })
             }
            }
          }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      相关资源
      最近更新 更多