简体   繁体   中英

Detect tap on a transformed CAShapeLayer’s path

I've got multiple CAShapeLayers on a view and all of them can be transformed using CATransform3D.

I've found a way to accurately detect taps that are on or inside the paths of these layers but it works only if the layers aren't transformed.

How can I make this work properly in all situations?

@objc private func userTapped(recognizer: UITapGestureRecognizer) {
        let tapLocation = recognizer.location(in: canvas)

        for shapeLayer in shapeLayers {
            guard let path = shapeLayer.path else { continue }

            let tempPath = path.copy(strokingWithWidth: CGFloat(lineWidth * 2), lineCap: .round, lineJoin: .round, miterLimit: .zero)

            if tempPath.contains(tapLocation) {
                // DO SOMETHING
            }
        }
    }

You need to get the tap location in the transformed layer's own geometry.

@objc private func userTapped(recognizer: UITapGestureRecognizer) {
    let canvasLocation = recognizer.location(in: canvas)

    for shapeLayer in shapeLayers {
        guard let path = shapeLayer.path else { continue }
        let shapeLocation = shapeLayer.convert(canvasLocation, from: canvas.layer)

        let tempPath = path.copy(strokingWithWidth: CGFloat(lineWidth * 2), lineCap: .round, lineJoin: .round, miterLimit: .zero)

        if tempPath.contains(shapeLocation) {
            // DO SOMETHING
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM