9

I'm getting intermittent reports from users on iOS 7 saying that the UIPanGestureRecognizer stops working on certain views every once in a while. They're supposed to be able to swipe a view to the right/left, but it just breaks and doesn't work for some unknown reason. Force quitting the app and relaunching it fixes the problem.

This problem never happened on iOS 6. And I don't have any code that disables the gesture recognizer at any time besides the gestureRecognizerShouldBegin delegate that forces the gesture to only recognize horizontal pans:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {

    if ([gestureRecognizer isMemberOfClass:[UIPanGestureRecognizer class]]) { 

        CGPoint translation = [gestureRecognizer translationInView:[self superview]];

        if (fabsf(translation.x) > fabsf(translation.y)) {

            if (translation.x > 0)
                return YES;
        }
    }
    return NO;
}

Did anything change in the UIPanGestureRecognizer (or just the plain UIGestureRecognizer) that could be causing this problem?

bmueller
  • 2,681
  • 1
  • 27
  • 45
  • Have you verified that the `x > y` check isn't causing an early exit during the times the gesture recognizer appears to be not working? – RyanR Oct 18 '13 at 14:12
  • Unfortunately I can't even reproduce the bug on any of my devices, but I've received enough reports from users that I know it's not just a single user who doesn't know what they're doing. – bmueller Oct 19 '13 at 04:45
  • 1
    I've got it to happen a few times now. The `gestureRecognizerShouldBegin` gets called and returns `YES` correctly, but the method I assign to the `UIPanGestureRecognizer` is NOT getting called. So it recognizes the gesture but does not call the method... – bmueller Oct 26 '13 at 00:07
  • 1
    @bmueller did you find a solution for this? We've also seen this many times (with UITapGestureRecognizers), but no reliable way to reproduce this. It pops up on different places each time. – Shai Nov 07 '13 at 15:44
  • Nothing yet, unfortunately :\ – bmueller Nov 07 '13 at 18:21
  • Have a look at comments on my question (it seems to be the same one) http://stackoverflow.com/questions/19126391/problems-with-gesture-recognizer-in-ios-7 Some answers may help in certain cases – Kup Nov 10 '13 at 16:50

2 Answers2

12

I think I finally solved this issue. Apparently iOS 7 handles gestures in subviews differently than it did in iOS 6 and earlier. To handle this, Apple implemented a new delegate:

(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

If you return YES, that should get your gesture recognizer to work. I've implemented it and haven't had any issues so far (though admittedly this was a rare bug that I could never reliably reproduce, so it's possible that it just hasn't recurred yet).

For more information, see https://stackoverflow.com/a/19892166/1593765.

Community
  • 1
  • 1
bmueller
  • 2,681
  • 1
  • 27
  • 45
  • I have this bug too with UIPanGestureRecognizer, direction not work in left/right delegate return always NO, in some kind images, i can't find explain( but when I change delegate method on this answer all works. Thank u! – dev.nikolaz Aug 05 '14 at 16:12
  • Awesome tip! Spend quite a couple of hours on this one! – Rudolf J Mar 14 '15 at 18:27
0

Why would you return NO in gesture recognizer just because on gestureRecognizerShouldBegin: the movement is only vertical? Since it's gesture made by user with his finger (and not made by a machine), there will always be some randomness in it's movement due to inaccuracy of moving finger. gestureRecognizerShouldBegin: will be called just after user touches the screen and the translation you get might be just a few pixels. Your recognizer will fail if user i.e. when putting his finger on screen moves it 2 pixels up, even if he then moves it 200 pixels to the right. This shouldn't cause the gesture recognizer to be permanently disabled but you should look into it as well, since it might confuse users when their gestures are not recognized for seemingly no reason.

johnyu
  • 2,152
  • 1
  • 15
  • 33