Disable gesture recognizer
Asked Answered
T

3

42

I have two types of recognizer, one for tap and one for swipe

UIGestureRecognizer *recognizer;

//TAP
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(numTap1:)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];

//SWIPE RIGHT
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
self.swipeRightRecognizer =(UISwipeGestureRecognizer *)recognizer;
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightRecognizer];
self.swipeRightRecognizer = (UISwipeGestureRecognizer *)recognizer;
[recognizer release];

with this function I can disable taps on some objects.

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

if ((touch.view == loseView) || (touch.view == subBgView) || (touch.view == btnAgain)) {

    return NO;
}

return YES;
}

How can I disable swipes?

Thanks a lot!

Thankyou answered 13/5, 2011 at 11:33 Comment(0)
B
124

UIGestureRecognizer has a property named enabled. This should be good enough to disable your swipes:

swipeGestureRecognizer.enabled = NO;

Edit: For Swift 5

swipeGestureRecognizer.isEnabled = false
Bowie answered 13/5, 2011 at 11:47 Comment(2)
@PeyloW: +1 I thought I would need to remove it and re-add it on the view.Obe
Additionally you can set userInteractionEnabled = NO on the view that the gesture is attached to. This is handy if you're using some sort of overlay that you're going to want to disable the underlying content anyway. This will also automatically get toggled based on hidden and alpha values for the view.Matthias
A
17

Why don't you set the delegate for the swipe gesture recognizer too and handle them within the same delegate method.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ( [gestureRecognizer isMemberOfClass:[UITapGestureRecognizer class]] ) {
        // Return NO for views that don't support Taps
    } else if ( [gestureRecognizer isMemberOfClass:[UISwipeGestureRecognizer class]] ) {
        // Return NO for views that don't support Swipes
    }

    return YES;
}
Acrodont answered 13/5, 2011 at 12:13 Comment(0)
A
0

I have a similar issue. Some of my disabled users tap and swipe at the same time, so the app moves to the next screen. I set up an option to allow them to use a three-fingered tap instead. I need to invoke the option the popoverControllerDidDismissPopover delegate and when the app first starts. So I wrote a method that combines the answers above. It looks for all of the swipe gesture recognizers and turns them off, then turns on my tap gesture recognizer.

- (void)changeGestureRecognizer {
    // Three finger tap to move to next screen
    if ([Globals sharedInstance].useDoubleTapToMoveToNextScreen) {

        // Let’s see which gestures are active and turn off the swipes
        for (UIGestureRecognizer *gestureRecognizer in self.view.gestureRecognizers) {
            NSLog(@"The gestureRecognizer is %@", gestureRecognizer);
            if ( [gestureRecognizer isMemberOfClass:[UISwipeGestureRecognizer class]] ) gestureRecognizer.enabled = NO;
        }
        // Add the three finger tap
        UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeNext)];
        [twoFingerTap setNumberOfTapsRequired:1];
        [twoFingerTap setNumberOfTouchesRequired:3];
        [self.view addGestureRecognizer:twoFingerTap];

    }
}
Autolysin answered 21/4, 2014 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.