user5032033
user5032033

Reputation:

How to detect which image has been tapped in swift

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?

Upvotes: 3

Views: 4683

Answers (1)

Arbitur
Arbitur

Reputation: 39081

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:"))

Upvotes: 6

Related Questions