I\'m quite new to iOS Development (so please forgive my ineptitude - I\'ve looked everywhere!), and was looking to find a way to detect a tap on a CAShapeLayer.
I used Arbitur's code and i had some errors. Here is a code i had with no errors. For swift 3.2 / 4.0
override func viewDidLoad() {
super.viewDidLoad()
let layer = CAShapeLayer()
layer.anchorPoint = CGPoint.zero
layer.path = UIBezierPath.init(ovalIn: CGRect(x: 0, y: 0, width: 100, height: 200)).cgPath
layer.bounds = (layer.path?.boundingBox)! // IMPORTANT, without this hitTest wont work
layer.fillColor = UIColor.red.cgColor
self.view.layer.addSublayer(layer)
}
// Check for touches
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
let point = touches.first?.location(in: self.view) // Where you pressed
if let layer = self.view.layer.hitTest(point!) as? CAShapeLayer { // If you hit a layer and if its a Shapelayer
if (layer.path?.contains(point!))! { // Optional, if you are inside its content path
print("Hit shapeLayer") // Do something
}
}
}