Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass taps through a UIPanGestureRecognizer

I'd like to detect swipe on the entire screen, however, the screen contains UIButtons, and if the user taps one of these buttons, I want the Touch Up Inside event to be triggered. I've create a UIView on the top of my screen, and added a UIPanGestureRecognizer on it to detect the swipe, but now I need to pass the gesture through that view when I detect that it's a tap rather than a swipe.

I know how to differentiate the gestures, but I've no idea on how to pass it to the view below.

Can anyone help on that? Thanks!

like image 350
Antoine Pastor Avatar asked Apr 17 '13 10:04

Antoine Pastor


3 Answers

Thanks for your answer. The link helped me to solve part of my problem. I've set the buttons as subviews of my gestureRecognizer view and I can now start a swipe from one of the buttons (and continue to use the buttons as well). I managed to prevent the buttons to go to the "down" state by using the following code :

UIPanGestureRecognizer *swipe = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetected:)];
swipe.maximumNumberOfTouches = 1;
swipe.delaysTouchesBegan =YES;
swipe.cancelsTouchesInView = YES;
[self.gestureRecognitionView addGestureRecognizer:swipe];
like image 196
Antoine Pastor Avatar answered Nov 05 '22 06:11

Antoine Pastor


there is a BOOL property of UIGestureRecognizer cancelsTouchesInView. default is yes. set it to NO , and the touches will pass thru to the UIView

also have a look at the solution for this question

like image 2
Bonnie Avatar answered Nov 05 '22 05:11

Bonnie


If you want to prevent the recognizer from receiving the touch at all, UIGestureRecognizerDelegate has a method gestureRecognizer:shouldReceiveTouch: you can use:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    // don't override any other recognizers
    if (gestureRecognizer != panRecognizer) {
        return YES;
    }

    CGPoint touchedPoint = [touch locationInView:self.someButton];

    return CGRectContainsPoint(self.someButton.bounds, touchedPoint) == NO;
}
like image 1
jszumski Avatar answered Nov 05 '22 05:11

jszumski