简体   繁体   中英

Limit Gesture Recognizer to Only One Specific UIView?

I have a UIView called myView on myViewController . I have a UIGestureRecognizer called swipeLeft (code below) that detects when a user swipes left on it.

The problem is: myViewController recognises the same gesture on the whole screen and performs another action. So I would like my app to perform myMethod when you swipeLeft in this particular area of myViewController , the area being myView .

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                           action:@selector(myMethod:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.delaysTouchesBegan = YES;
[self.myView addGestureRecognizer:swipeLeft];

More details: I am using RESideMenu and myViewController is the right menu, so when it is visible, the whole view of myViewController recognises swipes in all directions. I would like to change the recogniser in this particular UIView myView .

Thanks!

First you will need to add the swipe gesture to your view controllers header file.

@property (strong, nonatomic) UISwipeGestureRecognizer *swipeLeft;

If you look in DEMORootViewController.m, you will see this call:

self.rightMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"rightMenuViewController"];

This will call your awakeFromNib, and it's your first chance to do something. In here you will create that swipe gesture. You can not add it to your view yet though, because your outlets are not set at this point. The first time they are set is in viewDidLoad, so that's where you will add the gesture to your view. So add this to your view controllers implementation file

- (void)awakeFromNib
{
    self.swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(myMethod:)];   
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.swipeView addGestureRecognizer:self.swipeLeft];
}

- (void)myMethod:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"Did swipe") ;
}

Finally you will need to tell the pan gesture in RESideMenu.m to fail whenever our swipeLeft gesture occurs. The way to do that is to add the following code to RESideMenu.m at line 220. That's at the end of the viewDidLoad method.

if ([self.rightMenuViewController isKindOfClass:[DEMOVC class]]) {
            DEMOVC *rightVC = (DEMOVC *)self.rightMenuViewController;
            if (rightVC.swipeLeft) {
                [panGestureRecognizer requireGestureRecognizerToFail:rightVC.swipeGesture];
            } } }

This is assuming your custom VC is called DEMOVC. You will also need to import your custom VC to RESideMenu.m like this:

#import "DEMOVC.h"

Please let me know if this worked out for you, and if there's something else I can help you with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM