以下代码是在CAShapelayer上绘制的:
class LineView: UIView {
let rwPath = UIBezierPath()
let rwLayer = CAShapelayer()
let rwColor = UIColor(red: 11/255.0,green: 86/255.0,blue: 14/255.0,alpha: 1.0)
func setData() {
rwPath.move(to: CGPoint(x: 50.0,y: 50.0))
for i in 1...200 {
rwPath.addLine(to: CGPoint(x: i + 50,y: i + 50))
}
setUpRWLayer()
layer.addSublayer(rwLayer)
}
private func setUpRWLayer() {
rwLayer.path = rwPath.cgPath
rwLayer.fillColor = nil
rwLayer.lineCap = .butt
rwLayer.lineDashPattern = nil
rwLayer.lineDashPhase = 0.0
rwLayer.lineJoin = .miter
rwLayer.lineWidth = 3.0
rwLayer.miterLimit = 10.0
rwLayer.strokeColor = rwColor.cgColor
}
}
我已经尝试了一段时间以检测该行上的点击。我已经尝试过使用UITapGestureRecognizer并覆盖touchesBegin:withEvent函数,但是似乎没有任何效果,所以我来这里询问。
命中测试代码:
override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
let point = touches.first?.location(in: self)
if let hitLayer = self.layer.hitTest(point!) as? CAShapelayer {
if (hitLayer.path?.contains(point!))! {
print("Touched")
}
}
}