32

I currently have a custom UITableViewCell which contains a UIImageView and trying to add a UITapGestureRecognizer on the UIImageView with no luck. here is snippet of the code.

//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[tap release];

// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
    RKLogDebug(@"imaged tab");
}

I've also set userInteractionEnabled on the cell and the superview of the UIImageView but still no luck, any hints?

EDIT:

I've also remove cell's selection by cell.selectionStyle = UITableViewCellSelectionStyleNone; Could this be a problem

k06a
  • 17,755
  • 10
  • 70
  • 110
Herman
  • 3,004
  • 5
  • 37
  • 49
  • firstly, why aren't you using a UIButton instead of the image view? Secondly, did you enable user interaction for the actual image view? – Rog Oct 12 '11 at 04:25
  • @Rog 'cus I want to use UIImageView's UIViewContentModeScaleAspectFit function, does UIButton have the same functionality? – Herman Oct 12 '11 at 04:55
  • Also when using UIControl in scrollview it blocks the scrolling when the touch begin... – JakubKnejzlik Oct 14 '12 at 10:10
  • What is the variable name of the UIImageView? – Ray Shih Jan 09 '13 at 12:00
  • I would **make sure the parent views are all big enough** to display the sub views. Gestures only 'reach' the subviews if the touch area 'hits' the complete parent-subview chain. – gwest7 Nov 16 '16 at 07:03

3 Answers3

108

UIImageView's user interaction is disabled by default. You have to enable it explicitly to make it respond to touches.

imageView.userInteractionEnabled = YES;
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
6

Swift 3

This worked for me:

self.isUserInteractionEnabled = true
ƒernando Valle
  • 3,634
  • 6
  • 36
  • 58
2

In my case it looks like :

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = CELL_ROUTE_IDENTIFIER;
    RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[RouteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:cellIdentifier];
    }

    if ([self.routes count] > 0) {
        Route *route = [self.routes objectAtIndex:indexPath.row];

        UITapGestureRecognizer *singleTapOwner = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(imageOwnerTapped:)];
        singleTapOwner.numberOfTapsRequired = 1;
        singleTapOwner.cancelsTouchesInView = YES;
        [cell.ownerImageView setUserInteractionEnabled:YES];
        [cell.ownerImageView addGestureRecognizer:singleTapOwner];
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

And selector :

- (void)imageOwnerTapped:(UISwipeGestureRecognizer *)gesture {
    CGPoint location = [gesture locationInView:self.tableView];
    NSIndexPath *tapedIndexPath = [self.tableView indexPathForRowAtPoint:location];
    UITableViewCell *tapedCell  = [self.tableView cellForRowAtIndexPath:tapedIndexPath];

    NSIndexPath *indexPath = [self.tableView indexPathForCell:tapedCell];
    NSUInteger index = [indexPath row];

    Route *route = [self.routes objectAtIndex:index];
}
levo4ka
  • 2,248
  • 1
  • 19
  • 32