0

Arc在 SwiftUI 中创建了一个自定义形状,但是当我onTapGesture向它添加一个修饰符时,它会在我为 绘制的区域之外注册点击Arc(它在用于绘制 的区域内的任何地方注册点击)。我怎样才能确保水龙头只在绘制的路径注册,而不是整个?rectArcArcrect

这是Arc我画的形状:

struct Arc: Shape {
  let angle: Angle
  let thickness: CGFloat
  let clockwise: Bool
  
  func path(in rect: CGRect) -> Path {
    var path = Path()
    
    let innerArcEndPoint = CGPoint(x: rect.maxX - thickness, y: 0)
    let innerArcStartPoint = CGPoint(
      x: innerArcEndPoint.x * cos(CGFloat(angle.radians)),
      y: innerArcEndPoint.x * sin(CGFloat(angle.radians)))
    
    path.move(to: innerArcStartPoint)
    path.addArc(center: rect.origin, radius: innerArcEndPoint.x, startAngle: angle, endAngle: Angle.zero, clockwise: !clockwise)
    path.addLine(to: CGPoint(x: rect.maxX, y: 0))
    path.addArc(center: rect.origin, radius: rect.maxX, startAngle: Angle.zero, endAngle: angle, clockwise: clockwise)
    path.closeSubpath()
    
    return path
  }
}

这是我使用Arc带有onTapGesture修饰符的:

struct TestTappingArc: View {
    var body: some View {
        Arc(angle: Angle(degrees: 45), thickness: 20, clockwise: false)
          .foregroundColor(.blue) // The only area I want to be tappable
          .background(Color.orange) // The area I DON'T want to be tappable (but currently is tappable)
          .frame(width: 100, height: 100)
          .onTapGesture {
            print("hello")
          }
    }
}

这是它在屏幕上的样子:

在此处输入图像描述

4

2 回答 2

1

您需要使用contentShape相同的形状来限制命中测试区域(因为视图始终是矩形),例如

Arc(angle: Angle(degrees: 45), thickness: 20, clockwise: false)
  .foregroundColor(.blue)
  .contentShape(Arc(angle: Angle(degrees: 45), thickness: 20, clockwise: false))
  .onTapGesture {      // << here !!
    print("hello")
  }
  .background(Color.orange)
  .frame(width: 100, height: 100)
于 2021-07-23T04:57:20.960 回答
0

感谢 Asperi 的回答——这确实解决了问题。但是,我意识到一个更简单的解决方案是将背景移动到修饰符之后,如下所示onTapGesture

Arc(angle: Angle(degrees: 45), thickness: 20, clockwise: false)
  .foregroundColor(.blue)
  .frame(width: 100, height: 100)
  .onTapGesture {
    print("hello")
  }
  .background(Color.orange)
于 2021-07-23T15:41:13.743 回答