gpt4 book ai didi

swift - ZStack 中的手势阻止按钮

转载 作者:行者123 更新时间:2023-12-04 01:38:40 31 4
gpt4 key购买 nike

我在 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com