Sobes.tech
Middle

Jak určit, že uživatel klikl na vrstvu (layer)?

sobes.tech AI

Odpověď od AI

Použijte metodu hitTest: na UIView, do které je tento vrstva vložena, nebo zkontrolujte, zda bod kontaktu patří do rámce vrstvy pomocí containsPoint: nebo hitTest:.

Příklad detekce doteku na vrstvě uvnitř 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) // Převod bodu na souřadnice vrstvy

    if myLayer.contains(locationInLayer) {
        // Uživatel klikl na myLayer
        print("Vrstva kliknuta!")
    }
}