swift - ZStack 中的手势阻止按钮

标签 swift swiftui gesture

我在 ZStack 内的 Rectangle 中添加了一个手势。我有一个 VStack,它是按钮的 View 或菜单。当我将手势添加到矩形时(因为我需要检测菜单按钮后面那个 View 上的点击),然后按钮无法从设备上的用户接收交互。我在按钮后面有矩形,它们确实显示在矩形上方。在模拟器上它工作得很好,但在设备上却没有。社区将不胜感激任何帮助。谢谢!

var body: some View {
        ZStack {
            Rectangle()
                .foregroundColor(Color.black.opacity(0.01))
                .gesture(DragGesture(minimumDistance: 0)
                    .onChanged({ (value) in
                    self.store.tapped = true
                }))
            VStack(alignment: .leading, spacing: 0) {
                //menu buttons
                HStack(alignment: .center, spacing: 18.5) {
                    someView()
                        .environmentObject(self.anotherStore)
                        .frame(width: 145, height: 22)
                    self.otherSubviews()
                }
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 37, alignment: .leading)
                .padding(EdgeInsets(top: 0, leading: 6, bottom: 0, trailing: 6))
                //menu detail view
                self.detailView()
                }
            .padding(EdgeInsets(top: 6, leading: 6, bottom: 6, trailing: 12))
        }
    }

我已经尝试将 .zIndex 的 .zIndex 为 Rectangle 更改为 -1 为更高的菜单 View 。它不会改变任何东西

我希望 Rectangle 接收来自用户的交互(点击),并且按钮的 VStack 也将接收来自用户的交互。

最佳答案

发现背景中有一个有手势的 View ,你需要确保你的其他 View 有 .contentShape(Specify A Shape) .

在 Apple 文档中,它指出

    /// Returns a new view that defines the content shape for
    /// hit-testing `self` as `shape`.

我加了 .contentShape(Rectangle())VStack它允许点击按钮的菜单 View 以及允许点击背景中的矩形 View 。

用答案更新代码
var body: some View {
        ZStack {
            Rectangle()
                .foregroundColor(Color.black.opacity(0.01))
                .gesture(DragGesture(minimumDistance: 0)
                    .onChanged({ (value) in
                    self.store.tapped = true
                }))
            VStack(alignment: .leading, spacing: 0) {
                //menu buttons
                HStack(alignment: .center, spacing: 18.5) {
                    someView()
                        .environmentObject(self.anotherStore)
                        .frame(width: 145, height: 22)
                    self.otherSubviews()
                }
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 37, alignment: .leading)
                .padding(EdgeInsets(top: 0, leading: 6, bottom: 0, trailing: 6))
                //menu detail view
                self.detailView()
                }.contentShape(Rectangle()) //Define a tappable area
            .padding(EdgeInsets(top: 6, leading: 6, bottom: 6, trailing: 12))
        }
    }

关于swift - ZStack 中的手势阻止按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58493898/

相关文章:

arrays - 为 OSX 创建并定义 NSButton 数组

swiftui - 有条件创建形状 - SwiftUI

ios - Swift 包中 SwiftUI 热重载的 "noWorkspaceArena"错误

ios - 如何更改 SceneDelegate 中的 @ObservedObject 变量?

android - 无法在 Fling 上使用 java.lang.NullPointerException 启动 Activity

ios - 为什么 MagnificationGesture 在 SwiftUI 中只更新一次状态?

ios - 我的 iOS 应用程序经常崩溃 [UIGestureRecognizer _delegateShouldReceiveTouch :] of pan gesture

ios - 如何最好地Firebase查询来观察Bool属性的变化

swift - UIActivityviewcontroller 中的文本不可编辑

ios - 简单的 Swift Stepper 错误