.contentShape 在覆盖时失去效果
.contentShape losing effect when overlayed
我发现 .contentShape 在其他也接受手势交互的形状之上时的工作方式不同。
以下面的示例代码为例:
ZStack {
// Circle()
// .offset(y: -200)
// .gesture(TapGesture().onEnded { print("TAPPED CIRCLE") } )
let path = Path() { path in
path.move(to: CGPoint.zero)
path.addLine(to: CGPoint(x: 1000, y: 1000))
}
path
.stroke(Color.blue, style: StrokeStyle(lineWidth: 2))
.contentShape(path.stroke(style: StrokeStyle(lineWidth: 10)))
.gesture(TapGesture().onEnded { print("TAPPED PATH") })
}
按原样使用此代码,路径上的 contentShape 将使点击从很远的距离被识别。
取消注释 Circle 的代码,contentShape 突然没有相同的效果,现在点击需要正好在路径上才能工作。
为什么会这样?即使存在“Circle 代码”,我能否以某种方式获得相同的行为?
我做了一些额外的调试,发现它工作正常。但是,行为 是 不同的,这取决于圆圈是否在后面。
这是我额外的“调试”:
ZStack {
Circle()
.offset(y: -200)
.gesture(TapGesture().onEnded { print("TAPPED CIRCLE") } )
let path = Path() { path in
path.move(to: CGPoint(x: 250, y: 0))
path.addLine(to: CGPoint(x: 250, y: 1000))
}
path
.stroke(Color.green, style: StrokeStyle(lineWidth: 40))
path
.stroke(Color.blue, style: StrokeStyle(lineWidth: 2))
.contentShape(path.stroke(style: StrokeStyle(lineWidth: 40)))
.gesture(TapGesture().onEnded { print("TAPPED PATH") })
}
所有这一切都是为了添加一个额外的路径,使用与 contentShape 相同的线宽绘制笔划,这样我就可以看到点击在路径上的注册和不注册发生了变化。
路径越过圆圈的地方,命中检测实际上 比没有越过圆圈的地方 更好。
似乎当下面没有形状时,也接受手势交互,在识别手势的地方有一个额外的缓冲区。
丢失缓冲区并因此改变行为,这让我认为存在问题。
我发现 .contentShape 在其他也接受手势交互的形状之上时的工作方式不同。
以下面的示例代码为例:
ZStack {
// Circle()
// .offset(y: -200)
// .gesture(TapGesture().onEnded { print("TAPPED CIRCLE") } )
let path = Path() { path in
path.move(to: CGPoint.zero)
path.addLine(to: CGPoint(x: 1000, y: 1000))
}
path
.stroke(Color.blue, style: StrokeStyle(lineWidth: 2))
.contentShape(path.stroke(style: StrokeStyle(lineWidth: 10)))
.gesture(TapGesture().onEnded { print("TAPPED PATH") })
}
按原样使用此代码,路径上的 contentShape 将使点击从很远的距离被识别。
取消注释 Circle 的代码,contentShape 突然没有相同的效果,现在点击需要正好在路径上才能工作。
为什么会这样?即使存在“Circle 代码”,我能否以某种方式获得相同的行为?
我做了一些额外的调试,发现它工作正常。但是,行为 是 不同的,这取决于圆圈是否在后面。
这是我额外的“调试”:
ZStack {
Circle()
.offset(y: -200)
.gesture(TapGesture().onEnded { print("TAPPED CIRCLE") } )
let path = Path() { path in
path.move(to: CGPoint(x: 250, y: 0))
path.addLine(to: CGPoint(x: 250, y: 1000))
}
path
.stroke(Color.green, style: StrokeStyle(lineWidth: 40))
path
.stroke(Color.blue, style: StrokeStyle(lineWidth: 2))
.contentShape(path.stroke(style: StrokeStyle(lineWidth: 40)))
.gesture(TapGesture().onEnded { print("TAPPED PATH") })
}
所有这一切都是为了添加一个额外的路径,使用与 contentShape 相同的线宽绘制笔划,这样我就可以看到点击在路径上的注册和不注册发生了变化。
路径越过圆圈的地方,命中检测实际上 比没有越过圆圈的地方 更好。
似乎当下面没有形状时,也接受手势交互,在识别手势的地方有一个额外的缓冲区。
丢失缓冲区并因此改变行为,这让我认为存在问题。