简体   繁体   English

SwiftUI 多个图像点击手势如何在图像单击时执行 fullScreenCover

[英]SwiftUI multiple Images tap gesture how to do fullScreenCover on image click

I have 2 images that have onTapGesture .我有 2 张具有 onTapGesture 的图像。 When I tap an image I can see that only the tapped image is clicked however my issue is with fullScreenCover .当我点击一个图像时,我可以看到只有点击的图像被点击,但是我的问题是fullScreenCover If I click any image it always defaults to the PersonMain image view which then goes to InboxView() .如果我单击任何图像,它总是默认为PersonMain图像视图,然后转到InboxView() I have tried putting the fullScreenCover inside the onTapGesture but that does not work .我曾尝试将 fullScreenCover 放在 onTapGesture 中,但这不起作用。 Any suggestions would be great since I'm new to SwiftUI .任何建议都会很棒,因为我是 SwiftUI 的新手。 I've also set the isOpen State to false and the turned it to true on Tap however the first Image View InBoxView() is always the one popping up .我还将 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()
                   })
         }
        }
      }
   }

It needs to use separate states for each of fullScreenCover :它需要为每个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()
                   })
         }
        }
      }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM