22

My app has a table view (with a scroll-into of course) and this view slides on and off with a gesture recognizer (like on the Facebook app).

If I use a button to slide [the table view onto the screen], it works fine but when I use a gesture recognizer, the table view can't be scrolled anymore.

Here is the code of gesture recognizer with the problem:

[self.view addGestureRecognizer:self.slidingViewController.panGesture];

Somebody have an idea?

user2576304
  • 231
  • 1
  • 2
  • 4

5 Answers5

46

Your gesture is probably preventing the scroll view gesture from working because by default only 1 gesture can be recognising at a time. Try adding yourself as the delegate of your gesture and implementing:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

self.slidingViewController.panGesture.delegate = self;

also, add <UIGestureRecognizerDelegate> to the list of protocols you implement

Wain
  • 118,658
  • 15
  • 128
  • 151
  • thank you a lot for this answer! But I'm not expert in xcode so when I have implemented this code what I need to do after? – user2576304 Jul 12 '13 at 13:28
  • So you added your controller as the `delegate` of the gesture and implemented the above method and you still have problems? – Wain Jul 12 '13 at 13:31
  • How can i add my controller as the delegate? this notion of delegate is unknown for me i'm sorry – user2576304 Jul 12 '13 at 13:35
  • Thank you a lot it works now :D I have a other question it's less important but if you have the answer it will be great: this gestureRecognizer works fine but when i have added an navigation controller, it had the consequence that the slide to hide the view work but impossible to slide the view in the other way to show completely the view. Have you an idea of why? – user2576304 Jul 12 '13 at 14:42
  • What do you mean "added a navigation controller"? Where to and how? – Wain Jul 12 '13 at 15:05
  • On my tableViewController which slide on and off i have added a navigation controller: embed in -> navigation controller – user2576304 Jul 12 '13 at 15:34
14

I have used UIPangesture in my UItableview and to avoid this gesture I have used below delegate,

//This method helped me stopped up/down pangesture of UITableviewCell and allow only vertical scroll
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
        let translation = panGestureRecognizer.translationInView(superview)
        if fabs(translation.x) > fabs(translation.y) {
            return true
        }
        return false
    }
    return false
}
Pravin Kulkarni
  • 253
  • 2
  • 9
10

Here is the swift version:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
Swinny89
  • 7,273
  • 3
  • 32
  • 52
4

I had same issue of defining long press gesture on table view and not being able to scroll table when I long press on it.

Fixed by:

1- adding UIGestureRecognizerDelegate

2- adding gesture.delegate = self (after you defined the long press gesture)

3- adding this function:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {return true}

MaziB
  • 41
  • 2
  • 1
    Any recommendations for how to adapt this when the gesture recognizer is in a custom label class being used by the tableview? :-| – lucius degeer Nov 29 '17 at 02:07
1

If I get it right the view that you're adding the gesture recognizer to is the table view. By default the UIScrollView (and implicitly UITableView) class uses the pan gesture recognizer for the scroll and your gesture recognizer interferes with that. If you use another view as a container for the table view and you're adding the pan gesture recognizer to it should work.

alexbumbu
  • 321
  • 3
  • 10