Search code examples
cocos2d-iphone

Cancel the gesture handler temporary


I get gesture like this in mainScene

// listen for swipes to the left
UISwipeGestureRecognizer * swipeLeft= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[[[CCDirector sharedDirector] view] addGestureRecognizer:swipeLeft];

// listen for swipes to the right
UISwipeGestureRecognizer * swipeRight= [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[[[CCDirector sharedDirector] view] addGestureRecognizer:swipeRight];

and I added newLayer(CCNode) like layer on this mainScene

[mainScene addChild:newLayer];

When I swipe on newLayer, mainScene gesture handler is called.

I want to off the mainScene's swipe gesture handler during newLayer is added.

How can I make it ?


Solution

  • You might want to look into the gestureRecognizer:shouldReceiveTouch: or any of the other methods specified by the UIGestureRecognizerDelegate protocol as seen here UIGestureRecognizerDelegate Protocol Reference

    For example you might try...

    
    
    static NSMutableArray *_swipes;
    static BOOL _shouldCancel;
    
    - (void)viewDidLoad {
        _swipes = @[].mutableCopy;
        _shouldCancel = YES;
        for(UIGestureRecognizer *gesture in [mainScene gestureRecognizers]) {
            if([gesture isKindOfClass:[UISwipeGestureRecognizer class]]) {
                gesture.delegate = (id)self;
                [_swipes addObject:gesture];
            }
        }
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        return ![_swipes containsObject:gestureRecognizer] && _shouldCancel;
    }
    
    

    This way you can modify _shouldCancel to be NO or YES depending on when you want the swipe to be disabled.