我已经使用Bezier path创建了一个圆,并且我创建了点击事件来检查层是否已经被点击,它工作得很好,直到我增加了CAShapeLayer的lineWidth的大小。轻触lineWidth,有时会检测到,有时不会检测到。
我搜索了关于我遇到的问题的stackoverflow,但我无法解决它。问题仍然是一样的,我无法检测到我的图层(lineWidth区域)上的某些点击。
我只是想在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")
}
}发布于 2019-07-18 20:50:09
问题是,线条笔划并不是路径的一部分--它只是显示的一部分。通过使用一些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上设置这些属性。
希望这能有所帮助。祝好运。
https://stackoverflow.com/questions/57094541
复制相似问题