3

is it possible to let a specific gesture fail so the next possible gesture is recognized?

to be more specific, look at the sample snippet:

UISwipeGestureRecognizer *swipeLeft = [initialize UISwipeGestureRecognizer... @selector(handleSwipe:)]

swipeLeft = UISwipeGestureRecognizerDirectionLeft;

swipeLeft.delegate = self;

UIPanGestureRecognizer *pan = [initialize UIPanGestureRecognizer... @selector(handlePan:)]

pan.delegate = self;

[pan requireGestureRecognizerToFail:swipeLeft];

the above code states that if swipe left is not recognized by the device, pan gesture handler will be used.

so my question: is it possible to let swipeLeft intentionally fail (after being recognized as swipe left touch by the device) based on some criteria that is checked on handleSwipe, and let the pan gesture handle the touch input instead?

thanks.

Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
rgb
  • 33
  • 1
  • 4

2 Answers2

11

Check out the UIGestureRecognizerDelegate protocol here:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html

Specifically, the

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

method might be useful. If you simply return YES from this method, both gestures can be recognized at the same time, so you can respond properly to both.

Eric
  • 2,077
  • 17
  • 16
  • recognizing simultaneous gestures does not give me the desired behavior that i am aiming for. swipe left supposed to move UIView from one location to another and pan gesture moves it as well but based on your touch panning and velocity. thanks. – rgb Aug 28 '13 at 15:36
  • It sounds like the pan recognizer can let you move the view with your touch, but then if a swipe is detected, you can set a flag telling the pan to stop working, as the swipe behavior will take over. Do you think that might accomplish what you're trying to do? – Eric Aug 28 '13 at 16:28
  • that's a great idea and it is what i am trying to achieve, is there a simpler implementation without writing or overriding your own touchesBegan, Moved, Ended methods to flag and let the current gesture stop and pass to the next gesture behavior? thanks. – rgb Aug 28 '13 at 17:08
  • Yes, what I'm suggesting is to keep your gesture methods, return `YES` from that delegate method I mentioned, and then have an internal class property (a `BOOL` should be enough, I think) like `didSwipe`. In the beginning of your swipe recognizer method, set it to `YES` - this will only happen when a swipe is recognized. In your pan method, wrap the whole thing in an if statement: `if (!self.didSwipe) {...}`, so this part will only be executed if there wasn't a swipe or hasn't been a swipe yet. You may need to also take care of resetting that variable at some point. – Eric Aug 28 '13 at 17:32
  • thank you for your suggestion, i think the solution will work. will implement it and see if i can get the desired behavior. – rgb Aug 28 '13 at 18:28
0

Assuming you have some other handler implemented for the pan gesture, can't you just do:

-(void)handleSwipe:(id)sender {

    if //criteria is met to ignore left swipe
    {
        [self handlePan:self];   
    }
}

-(void)handlePan:(id)sender {

    // handle pan gesture here

}
james00794
  • 1,137
  • 7
  • 14
  • this could work but handlePan (with its own implementation of touchesBegan, Moved, Ended which is a bit complex for a noob) is actually on a different library being used on view controller. thanks. – rgb Aug 28 '13 at 15:27