UITapGestureRecognizer on UIImageView within UITablevlewCell not getting called
Asked Answered
A

3

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

Alimentation answered 12/10, 2011 at 4:23 Comment(5)
firstly, why aren't you using a UIButton instead of the image view? Secondly, did you enable user interaction for the actual image view?Retool
@Retool 'cus I want to use UIImageView's UIViewContentModeScaleAspectFit function, does UIButton have the same functionality?Alimentation
Also when using UIControl in scrollview it blocks the scrolling when the touch begin...Wagram
What is the variable name of the UIImageView?Subroutine
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.Ictus
P
108

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

imageView.userInteractionEnabled = YES;
Picrate answered 12/10, 2011 at 4:52 Comment(2)
Maybe you have an UIView above it ?Reshape
Nice. It help for me.Relationship
F
6

Swift 3

This worked for me:

self.isUserInteractionEnabled = true
Fard answered 8/2, 2017 at 21:7 Comment(0)
P
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];
}
Poikilothermic answered 20/7, 2015 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.