Detecting a tap on a CAShapeLayer in Swift?

前端 未结 4 571
时光取名叫无心
时光取名叫无心 2020-12-29 13:18

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.

4条回答
  •  滥情空心
    2020-12-29 14:04

    Swift 4 & 5

    My UIImageView has multiple embedded CAShapeLayer objects. Here is how I was able to detect taps on them. Referenced from this tutorial.

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        let touch = touches.first
    
        guard let point = touch?.location(in: imageView) else { return }
        guard let sublayers = imageView.layer.sublayers as? [CAShapeLayer] else { return }
    
        for layer in sublayers {
            if let path = layer.path, path.contains(point) {
                print(layer)
            }
        }
    }
    

提交回复
热议问题