Poate fi atasat un UIGestureRecognizer la mai multe view-uri?

Cei de la Apple spun ca nu: un UIGestureRecognizer poate fi utilizat doar cu un singur view.
Cum trecem peste acest impediment, avand o lista de view-uri, fiecare din aceste view-uri facand aceeasi actiune, sa zicem tiparirea campului tag?

1. generam o functie care va returna un UIGestureRecognizer

func setGestureRecognizer() -> UITapGestureRecognizer {
var gesture = UITapGestureRecognizer()
gesture = UITapGestureRecognizer(target: self, action: #selector(changeImage(sender:)))
return gesture
}

2. definim functia de raspuns in care extragem view-ul senderul UIImageView
func changeImage(sender: UITapGestureRecognizer) {
let tappedImageView = sender.view! as? UIImageView
print("button clicked - \(tappedImageView!.tag)")
}

3. folosim recognizer-ul in view-urile noastre
view1.addGestureRecognizer(setGestureRecognizer())
view2.addGestureRecognizer(setGestureRecognizer())
view3.addGestureRecognizer(setGestureRecognizer())

Share Button

Stefan

Leave a Reply

Your email address will not be published. Required fields are marked *