-1

I have 5 ImageViews in my View and I have the IBOutlets for all of these.

I have a single tap gesture recognizer which I have set for all of them.

Now In the action for this gesture recognizer I need to find out which image has been tapped.

Please let me know the way to achieve this. Please note I am using the storyboard to add all these images and the gesture recognizer.

Thanks.

rkb
  • 10,933
  • 22
  • 76
  • 103
  • Your question makes no sense. You cannot set a single tap gesture recognizer for five different views. It has just one view. If you have five tappable image views, make five tap gesture recognizers to go with them. Now when a gesture recognizer fires, its `view` is the tapped image view. – matt Feb 11 '14 at 03:45

2 Answers2

1

UIImageView does not respond to touch gestures. You'll need to subclass it and then attach the gesture listener to each view. From here:

A UIImageView is derived from a UIView which is derived from UIResponder so it's ready to handle touch events. You'll want to provide the touchesBegan, touchesMoved, and touchesEnded methods and they'll get called if the user taps the image. If all you want is a tap event, it's easier to just use a custom button with the image set as the button image. But if you want finer-grain control over taps, moves, etc. this is the way to go.

You'll also want to look at a few more things:

Override canBecomeFirstResponder and return YES to indicate that the view can become the focus of touch events (the default is NO).

Set the userInteractionEnabled property to YES. The default for UIViews is YES, but for UIImageViews is NO so you have to explicitly turn it on.

If you want to respond to multi-touch events (i.e. pinch, zoom, etc) you'll want to set multipleTouchEnabled to YES.

Community
  • 1
  • 1
Jason
  • 13,563
  • 15
  • 74
  • 125
0

You should set tap gesture for every imageView. After this,you should set every imageView a unique tag. Then,you can specify the tapped imageView with tag.The imageView object can be achieved by property 'view' of UIGestureRecognizer.

mengxiangjian
  • 552
  • 2
  • 8