Search code examples
iphoneiosipaduigesturerecognizer

iOS overlapped views and UIGestureRecognizer


I have a view with a UIButton inside like this: snapshot 01 http://imageshack.us/a/img827/7240/screenshot20130220at333.png

The button is bound to an IBAction:

-(IBAction)deleteButtonPressed :(id)sender{
    NSLog(@"DeleteButtonPressed");
}

I also have another view that shows a collection of the last view: snapshot 02

In the controller of this view, at some point I have this:

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;
    [preview addGestureRecognizer:tapGesture];

And this:

- (void)tapAction:(UIGestureRecognizer *)gestureRecognizer{

    EditorViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"EditorViewController"];
    vc.narration = preview.narration;
    self.navigationItem.backBarButtonItem.style = UIBarButtonItemStylePlain;
    self.navigationItem.backBarButtonItem.title = @"Done";
    vc.navigationItem.backBarButtonItem.style = UIBarButtonItemStylePlain;
    vc.navigationItem.backBarButtonItem.title = @"Done";
    [self.navigationController pushViewController:vc animated:YES];
}

So, when I touch in the preview, I load another view. My problems come when I tap the button. In this case, the function -(IBAction)deleteButtonPressed: (id)sender doesn't get executed but the tapAction does. Also, the button change while is pressed. I tried to don't add the GestureRecognizer and then -(IBAction)deleteButtonPressed :(id)sender is executed.

What I am doing wrong? How can I achieve that when tap on the button, the view underneath doesn't capture the gesture?


Solution

  • You need to implement the shouldReceiveTouch method (which is part of the UIGestureRecognizerDelegate protocol).

    More info about your problem here:

    Gesture recognizer and button actions