SwiftUI 多个图像点击手势如何在图像单击上执行全屏覆盖

标签 swift swiftui

我有 2 个具有 onTapGesture 的图像。当我点击图像时,我可以看到只有点击的图像被点击,但我的问题是 fullScreenCover 。如果我单击任何图像,它总是默认为 PersonMain ImageView ,然后转到 InboxView() 。我尝试将 fullScreenCover 放入 onTapGesture 中,但这不起作用。任何建议都会很棒,因为我是 SwiftUI 的新手。我还将 isOpen State 设置为 false,并在点击时将其设置为 true,但是第一个 ImageView 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()
                   })
         }
        }
      }
   }

最佳答案

它需要为每个 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()
                   })
         }
        }
      }
   }

关于SwiftUI 多个图像点击手势如何在图像单击上执行全屏覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64903316/

相关文章:

ios - 如何在 iOS 中使用 Swift 运行命令行命令或任务?

iOS swift 应用程序在文本字段中点击崩溃(NSNull 长度 - 实例无法识别的选择器)

swift:向数据添加字节的正确方法

ios - 如何在 SwiftUI 中以横向模式预览设备?

swift - 在 iPad 上使用 SwiftUI NavigationView 在代码中切换侧边栏

swift - Xcode 8 with legacy Swift 2.3 : SourceKit imposes Swift 3. 0 rules & shows phantom errors

ios - 创建全局 NSOperationQueue

ios - SwiftUI 动画和后续的反向动画到原始状态

swift - 每当我的 TextView 更新时,我的 GameScene 都会重新加载

SwiftUI ForEach "Unable to infer complex closure return type; add explicit type to disambiguate"