denikov
denikov

Reputation: 869

separate tap gesture recognizers in uitableview header

Is there a simple solution to adding multiple UITapGestureRecognizers? I have a table view header which contains a UIImageView. Around the image view edges is clear space for the header. What I am trying to achieve is add a tap method for the header and tap method for the image view. But adding one to the header uses the entire header, including the image view. Is there a way to separate them?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(35, 10, 250, 250)];
    _imageView = imageView;
    _imageView.image = imageData;
    UITapGestureRecognizer *headerTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerTapped:)];
    _headerTap = headerTap;
    UITapGestureRecognizer *imagetap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapped:)];
    _imagetap = imagetap;
    [header addGestureRecognizer:_headerTap];
    [_imageView addGestureRecognizer:_imagetap];
    [header addSubview:_imageView];
    return header;
}

Upvotes: 2

Views: 2663

Answers (2)

Brandon
Brandon

Reputation: 2427

You can accomplish this by listening to the delegate methods of the gesture recognizer. Depending on how your views are set up you can use

(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

to see decide if you want your recognizer to fire. For instance We have an app with a drop down tableview to perform selections. We installed a two recognizers, one on the window so we can 'roll up' the drop down when the user taps outside of it, and one on the 'button' which is just the contents of a cell. We use this method to determine if the touch was outside of our visible tableviews bounds an if so we dismiss it.

The other relevant delegate method is

(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

From your description this seems like the best to use. From this you can tell your headers gesture recognizer not to fire alongside the one on your image view. For example

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{     
    if (gestureRecognizer == self.headerGestureReconizer && otherGestureRecognizer == self.imageViewGestRecognizer) {
        return NO;
    }

    return YES;
}

You can also set up dependencies within gesture recognizers via the instance method (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer. This is most useful when you have something like a single tap and a double tap recognizer on the same view and isn't what you want in this situation.

Upvotes: 1

Mundi
Mundi

Reputation: 80273

Make a transparent button covering the entire header.
Put the image view above it and cover that with another transparent button.

Upvotes: 3

Related Questions