Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tap Recognition for UIImageView in the UITableViewCell

Tags:

Currently I'm facing a problem, I would like to perform action when the UIImageView on my UITableViewCell had been tapped.

Question: How could I do it? Could any one show me the code, or any tutorial?

Thanks in advance!

like image 489
Anatoliy Gatt Avatar asked Jul 20 '12 08:07

Anatoliy Gatt


1 Answers

This is actually easier than you would think. You just need to make sure that you enable user interaction on the imageView, and you can add a tap gesture to it. This should be done when the cell is instantiated to avoid having multiple tap gestures added to the same image view. For example:

- (instancetype)initWithCoder:(NSCoder *)aDecoder {     if (self = [super initWithCoder:aDecoder]) {         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTapMethod:)];          [self.imageView addGestureRecognizer:tap];         [self.imageView setUserInteractionEnabled:YES];     }      return self; }  - (void)myTapMethod:(UITapGestureRecognizer *)tapGesture {     UIImageView *imageView = (UIImageView *)tapGesture.view;     NSLog(@"%@", imageView); } 
like image 186
Mick MacCallum Avatar answered Oct 19 '22 23:10

Mick MacCallum