当我点击另一个视图时,会触发onTapGesture修改器.

以下是我的观点,归结为一个最小的可重现的例子:

var body: some View {
  VStack {
    Menu {
      ForEach(["Option 1", "Option 2"], id: \.self) { item in
        Button(item) {
          currentItem = item
          expanded.toggle()
        }
      }
    } label: {
      HStack {
        Text(currentItem ?? "Pick something…")
          .animation(.default, value: currentItem)
        Spacer()
        Image(systemName: "chevron.down")
          .rotationEffect(expanded ? .degrees(-180.0) : .zero)
          .animation(.default, value: expanded)
      }
    }
      .menuOrder(.fixed)
      .onTapGesture { expanded.toggle() }
    Divider()
  }
}

@State private var currentItem: String?
@State private var expanded = false

(简而言之,有一个带有定制标签的菜单,当菜单展开和折叠时会显示动画.我知道也有DisclosureGroup个,但它的外观和行为有点不同,不符合我的应用程序的规格.)

When I tap on the Menu itself, everything works as expected: Menu proper animation

However, if I tap below the Menu, the area of Divider, the Menu is not triggered (expected,) but its onTapGesture unexpectedly is triggered and starts the chevron animation: Unexpected tap detection

I tried to move onTapGesture inside Menu's label, but it's not triggered from there at all: Not triggering example

有谁知道那里发生了什么,以及如何修复它?我现在唯一能想到的就是这是SwiftUI中的一个错误.如有任何指导,我们不胜感激.

推荐答案

Why is this happening?

这是因为您正在iPhone上测试它.IPhone有一个touch 屏,Tap手势捕捉到的手指点击测试比光标的尖端更大.

您可以按住选项键以显示手指点击测试并查看重叠部分.

Demo

⚠️第二个原因是您正在模拟多点触控

如果您在Mac环境中运行相同的代码,它将按照您的预期运行.

如果您将另一个可touch 元素放在此元素附近,系统将 Select 离手指较近的元素(如果不能同时touch )

解决方法

您可以在特定位置运行水龙头,例如:

let menuHeight: CGFloat = 32 // 👈 Define a height (or get the height using available methods) 
var body: some View {
    VStack {
        Menu {
            ForEach(["Option 1", "Option 2"], id: \.self) { item in
                Button(item) {
                    currentItem = item
                    expanded.toggle()
                }
            }
        } label: {
            HStack {
                Text(currentItem ?? "Pick something…")
                    .animation(.default, value: currentItem)
                Spacer()
                Image(systemName: "chevron.down")
                    .rotationEffect(expanded ? .degrees(-180.0) : .zero)
                    .animation(.default, value: expanded)
            }
            .frame(height: menuHeight) // 👈 Set the height (or just get the height using available methods)
        }
        .menuOrder(.fixed)
        .simultaneousGesture(
            SpatialTapGesture().onEnded { value in // 👈 Use this type of tap gesture
                if (0...menuHeight).contains(value.location.y) { // 👈 Limit the action in the valid range
                    expanded.toggle()
                }
            }
        )

        Divider()
    }
}

Ios相关问答推荐

Swift:从字符串目录获取带有参数的本地化字符串

将图案UI视图动画化以模拟进度条

AlGolia InstantSearch Swift Package Manager引发编译错误

有没有办法观察滚动的SwiftUI图表的内容偏移量?

如何在iOS中以编程方式推送另一个页面?

围绕对象旋转和移动相机不起作用Swift SceneKit

swift|在具有自定义单元格的多个TableView中没有任何内容

ITMS-90432:将应用上传到应用store 时出现问题

Flutter firebase_auth 中的无效应用程序凭据/令牌不匹配

AppCenter 错误:/usr/bin/xcodebuild 失败,返回码:65

Swift 图表:.chartYScale 似乎只适用于 100 的增量?

如何在不支持并发的自动关闭中修复'async'调用?

缺少推送通知权利

UITextView 从文本的底部或中间开始

使用 Xcode 4 的 iPhone 临时构建

iOS中的apk类似功能是什么?

文件是为存档而构建的,它不是被链接的体系 struct (i386)

UINavigationBar - 以编程方式设置标题?

什么是强属性属性

在 SKScene 中设置按钮