在 CAShapeLayer 列表中检测长按

Detecting long press in list of CAShapeLayer

我有一个在我的视图中具有可见 CAShapeLayer 的对象列表。

internal class Line {
    var layer = CAShapeLayer()
}

这些显示在 UIView 类型的对象上。在 UIView 我有一个 longPress 手势识别器,它应该检测 line.layers.

之一的长按

这是我得到的

@objc private func didLongPress(_ sender: UILongPressGestureRecognizer) {
    var selectedLine: Line?
    let touchPoint = sender.location(in: self)
    if sender.state == .began {
        for line in lines {
            if line.layer.contains(touchPoint) == true {
                print("true")
            } else {
                print("false")
            }

        }
    } else if sender.state == .changed {
        for line in lines {
            if line.layer.contains(touchPoint) == true {
                print("true")
            } else {
                print("false")
            }

        }

    } else if sender.state == .ended {
        for line in lines {
            if line.layer.contains(touchPoint) == true {
                print("true")
            } else {
                print("false")
            }

        }

    }
}

不用说了,只会打印false。有人有什么主意吗?

谢谢!

在以下位置找到解决方案:

line.layer.path?.boundingBox.contains(touchPoint)