sooper
sooper

Reputation: 6039

UIPanGestureRecognizer on UITableViewCell overrides UITableView's scroll view gesture recognizer

I've subclassed UITableViewCell and in that class I apply a Pan gesture recogniser:

UIPanGestureRecognizer *panning = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanning:)];
panning.minimumNumberOfTouches = 1;
panning.maximumNumberOfTouches = 1;
[self.contentView addGestureRecognizer:panning];
[panning release];

I then implement the delegate protocol which is supposed to allow simultaneous gestures in the table's view:

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

Then I place a log inside the handlePanning method just to see when it's detected:

- (void)handlePanning:(UIPanGestureRecognizer *)sender {
    NSLog(@"PAN");
}

My problem is that I'm not able to vertically scroll through the list of cells in the tableview and that handlePanning is called no matter which direction I pan.

What I want is for handlePanning to only be called when there is only horizontal panning and not vertical. Would appreciate some guidance.

Upvotes: 20

Views: 11137

Answers (4)

Till
Till

Reputation: 27597

Subclass the panning gesture recognizer and make it recognize only horizontal panning. There is a great WWDC 2010 video on the issue of custom gesture recognizers available. Actually there are two on that subject, check them out at https://developer.apple.com/videos/archive/:

  • Simplifying Touch Event Handling with Gesture Recognizers
  • Advanced Gesture Recognition

Upvotes: 3

yogesh wadhwa
yogesh wadhwa

Reputation: 721

Have you tried setting the bounces property to NO?

Upvotes: -1

Bharath
Bharath

Reputation: 324

Add the gesture recogniser On tableview. From that, you can get the cell object. From there you can handle the cell Functionality. For each gesture, there will be a begin, changed, end state. So, store the begin position.

    CGPoint beginLocation = [gesture locationInView:tblView]; // touch begin state.

    CGPoint endLocation = [gesture locationInView:tblView]; // touch end state.

Using this point, you can get the IndexPath

    NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:beginPoint];

From this indexpath, you can access the cell.

            UITableViewCell *cell = [tableview cellForRowAtIndexPath : indexPath];

Using this Cell object, you can handle it.

Upvotes: 2

Jack Greenhill
Jack Greenhill

Reputation: 10460

Have you tried setting pannings delegate property?

panning.delegate = /* class name with the delegate method in it */;

You'll also need to conform that class to UIGestureRecognizerDelegate.

Upvotes: 17

Related Questions