Sobes.tech
Middle

Kā noteikt, vai lietotājs ir noklikšķinājis uz slāņa (layer)?

sobes.tech AI

Atbilde no AI

Izmantojiet hitTest: metodi UIView, kurā ir iekļauts šis slānis, vai pārbaudiet, vai kontakta punkts pieder slāņa rāmi, izmantojot containsPoint: vai hitTest:.

Piemērs, kā noteikt pieskārienu uz slāņa touchesBegan iekšpusē:

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ārvērst punktu uz slāņa koordinātām

    if myLayer.contains(locationInLayer) {
        // Lietotājs pieskārās myLayer
        print("Slānis pieskāries!")
    }
}