我画了一个10点宽的路径,但是当我试图检测其中的touch 时,UIBezierPath包含:method只检测发生在1像素行而不是10像素行的touch .
let path = UIBezierPath()
path.move(to: start)
path.addLine(to: end)
path.usesEvenOddFillRule = true
path.lineWidth = 10
path.lineCapStyle = .round
path.stroke()
path.close()
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = lineColor.cgColor
shapeLayer.lineWidth = path.lineWidth
下面是一个检测touch 的方法:
@objc func didTapOnView(_ recognizer: UIGestureRecognizer) {
let tapLocation:CGPoint = recognizer.location(in: self)
self.hitTest(tapLocation: CGPoint(x: tapLocation.x.rounded(), y: tapLocation.y.rounded()))
}
private func hitTest(tapLocation: CGPoint){
let path: UIBezierPath = path0
print("Touch location is: \(tapLocation)")
if path.contains(tapLocation){
print("inside")
} else{
print("outside")
}
}
我只得到发生在精确的1像素线,而不是在10像素线的touch .如果我不做舍入,根本不会检测到任何touch .