在 CAShapeLayer lineWidth 上未检测到点击事件?
Taps events not getting detected on CAShapeLayer lineWidth?
我已经使用 Bezier 路径创建了一个圆,还创建了点击事件来检查图层是否已被点击,它工作正常,直到我增加 CAShapeLayer 的 lineWidth 的大小。通过点击 lineWidth,有时会检测到,有时不会检测到。
我在 Whosebug 上搜索了我遇到的问题,但我无法解决它。问题仍然存在,我无法检测到图层(线宽区域)上的某些点击。
我只想检测 CAShapeLayer 的 lineWidth 上的点击,我一直在到处搜索,但找不到合适的解决方案。大多数答案都是过时的语言。如果有人能举个例子来解决我的问题,我将不胜感激。 Swift 4.
https://i.imgur.com/194Aljn.png
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapDetected(tapRecognizer:)))
self.addGestureRecognizer(tapRecognizer)
@objc public func tapDetected(tapRecognizer:UITapGestureRecognizer) {
let tapLocation:CGPoint = tapRecognizer.location(in: self)
self.hitTest(tapLocation: CGPoint(x: tapLocation.x, y: tapLocation.y))
}
private func hitTest(tapLocation:CGPoint) {
if layer.path?.contains(tapLocation) == true {
print("Do something")
} else {
print("Nothing Found")
}
}
问题是线条并不是路径的一部分——它只是它显示的一部分。您可以使用一些 CGPath 方法将路径转换为包含笔划的更大路径:
let pathWithLineStroke = UIBezierPath.init(cgPath: path.cgPath.copy(strokingWithWidth: 2.0, lineCap: CGLineCap.butt, lineJoin: .bevel, miterLimit: 1.0));
当然,请将 width
、lineCap
、lineJoin
和 miterLimit
替换为您的实际值。
我建议在您的代码中更早地执行此操作,然后只绘制已经内置笔划的路径,而不是在 CALayer 上设置这些属性。
希望对您有所帮助。祝你好运。
我已经使用 Bezier 路径创建了一个圆,还创建了点击事件来检查图层是否已被点击,它工作正常,直到我增加 CAShapeLayer 的 lineWidth 的大小。通过点击 lineWidth,有时会检测到,有时不会检测到。
我在 Whosebug 上搜索了我遇到的问题,但我无法解决它。问题仍然存在,我无法检测到图层(线宽区域)上的某些点击。
我只想检测 CAShapeLayer 的 lineWidth 上的点击,我一直在到处搜索,但找不到合适的解决方案。大多数答案都是过时的语言。如果有人能举个例子来解决我的问题,我将不胜感激。 Swift 4.
https://i.imgur.com/194Aljn.png
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapDetected(tapRecognizer:)))
self.addGestureRecognizer(tapRecognizer)
@objc public func tapDetected(tapRecognizer:UITapGestureRecognizer) {
let tapLocation:CGPoint = tapRecognizer.location(in: self)
self.hitTest(tapLocation: CGPoint(x: tapLocation.x, y: tapLocation.y))
}
private func hitTest(tapLocation:CGPoint) {
if layer.path?.contains(tapLocation) == true {
print("Do something")
} else {
print("Nothing Found")
}
}
问题是线条并不是路径的一部分——它只是它显示的一部分。您可以使用一些 CGPath 方法将路径转换为包含笔划的更大路径:
let pathWithLineStroke = UIBezierPath.init(cgPath: path.cgPath.copy(strokingWithWidth: 2.0, lineCap: CGLineCap.butt, lineJoin: .bevel, miterLimit: 1.0));
当然,请将 width
、lineCap
、lineJoin
和 miterLimit
替换为您的实际值。
我建议在您的代码中更早地执行此操作,然后只绘制已经内置笔划的路径,而不是在 CALayer 上设置这些属性。
希望对您有所帮助。祝你好运。