【问题标题】:SwiftUI: How to make a transparent Rectangle (.fill(.clear)) receive gestures?SwiftUI:如何使透明的 Rectangle (.fill(.clear)) 接收手势?
【发布时间】:2022-07-06 17:40:24
【问题描述】:

我想用几个Rectangle()s 覆盖我的图像,这应该响应手势(如点击或拖动)。但是,我发现当我清除矩形时,它会停止接收手势。

Rectangle()
  .fill(.clear)
  .gesture(
    LongPressGesture()
      .onEnded { value in 
        // this isn't called when the rectangle fill is .clear
      }
  )

有没有办法让不可见的元素接收点击?我知道我可以给它 1 % 的不透明度,但这感觉就像是一个丑陋(且可见)的杂物。

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    添加定义命中可测试区域的内容形状,例如

    Rectangle()
      .fill(.clear)
      .contentShape(Rectangle())   // << here !!
      .gesture(
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 2019-12-21
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多