0
votes

I have a couple of UITextField and UIButton inside a UIScrollView and this scroll view is inside a view of my UIViewController. I added a touch gesture recognizer to dismiss the keyboard if shown:

UITapGestureRecognizer *tapToDismissKeyboard = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    [tapToDismissKeyboard setCancelsTouchesInView:NO];
    tapToDismissKeyboard.delegate = self;
    [self.view addGestureRecognizer:tapToDismissKeyboard];

#pragma mark UITapGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.signupButton_] || [touch.view isDescendantOfView:self.profilePictureImageView_] || [touch.view isDescendantOfView:self.signupUsingFacebook_]) {
        return NO; // ignore the touch
    }

    return YES; // handle the touch
}

The issue is that when I tap the signin/signup button it still detects the tap gesture, in which I actually want the button touch.

3

3 Answers

0
votes

The touched view should be the button themselves, so doing

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view==self.signupButton_ ) {
        return NO; // ignore the touch
    }

    return YES; // handle the touch
}

should work for you, isDescendantOfView returns false because that view IS the button you are trying to have clicked...similarly if you want to just let any button through you can do

if([v isKindOfClass:[UIButton class]])
{
    return  NO;  
}

hope that helped

Daniel

0
votes

you are add your add Gesture Recognizer in whole view if you your button inside that view than it will not take button touch for that create insert all your content inside that view then add Gesture Recognizer for that view only and create another view for your button it will work.

0
votes

A work around for this would be to set gesture for button as well and put the code as

[tapToDismissKeyboard requireGestureRecognizerToFail:buttonTapGestureRecognizer];