How to detect which image has been tapped in swift

4.6k views Asked by At

I have created 6 UIImageViews on a ViewController, and I am later going to add TapGestureRecognizers to all of them.

I want to make it so that depending on what image has been clicked, another ViewController will open and display certain information.

For this to happen, I need to know which image has been clicked. How would I do this in Swift?

1

There are 1 answers

1
Arbitur On

UIGestureRecognizer has property 'view' this property is the view you add it to. For this example the imageView.

func tap(gesture: UIGestureRecognizer) {
    println(gesture.view!.tag) // You can check for their tag and do different things based on tag
}

let img = UIImageView()
img.userInteraction = true
img.tag = 0
img.addGestureRecognizer(UITapGestureRecognizer(self, action: "tap:"))