Sobes.tech
Middle

Ako určiť, či používateľ klikol na vrstvu (layer)?

sobes.tech AI

Odpoveď od AI

Použite metódu hitTest: na UIView, do ktorého je tento vrstvený, alebo skontrolujte, či bod kontaktu patrí do rámca vrstvy pomocou containsPoint: alebo hitTest:.

Príklad detekcie dotyku na vrstve v touchesBegan:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let locationInView = touch.location(in: self.view)
    let locationInLayer = self.view.layer.convert(locationInView, to: myLayer) // Prevod bodu na súradnice vrstvy

    if myLayer.contains(locationInLayer) {
        // Používateľ sa dotkol myLayer
        print("Vrstva dotknutá!")
    }
}