cannyboy cannyboy - 5 months ago 48
iOS Question

Passing vertical scrolling gesture to UITableView below

(edited for clarity)

I have a UITableView. On top of that is a UIView with a Pan gesture attached. This Pan swipes left and right to change the underlying table. I use the pan gesture's action method to move the table. That working fine.

However, the UIView & its Pan gesture interferes with up/down scrolling the UITableView. How can I send the up/down scrolling to the table and keep the left-right on the view's area?

---------------------------------------
| |
| ---------------------- |
| | | |
| | | |
| | | |
| UITableView | | |
| | UIView | |
| | + | |
| | PanGesture | |
| | | |
| | | |
| | | |
| | | |
| ---------------------- |
| |
| |
---------------------------------------


The method triggered by the Pan gesture is like this

-(void)move:(UIPanGestureRecognizer*)sender
{
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
float xTest = fabsf(translatedPoint.x);
float yTest = fabsf(translatedPoint.y);
if ( xTest>yTest)
{
// Move table view left-right.. this works
} else
{
// Send up-down scrolling gesture to table view????? How to?
}
}

Answer

I just solved a similar problem, except it was for vertical pans instead of horizontal ones. I'm not 100% sure about your use case, so this may not be what your looking for, but it may lead you in the right direction.

I sub-classed UIPanGestureRecognizer, and implemented the touchesMoved method, and checked to see whether the gesture had a larger horizontal or vertical change. Below is a snippet. The credit belongs to a different stackoverflow post, but I cannot find the link at the moment. (Sorry in advance for the poor formatting, first time posting)

-(void)touchesMoved:(NSSet*) touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if(self.state == UIGestureRecognizerStateFailed) return;
CGPoint currentPoint = [[touches anyObject] locationInView:self.view];
CGPoint prevPoint = [[touches anyObject] previousLocationInView:self.view];
moveX += prevPoint.x - currentPoint.x;
moveY += prevPoint.y - currentPoint.y;
if(!drag) {
    if(abs(moveY) > abs(moveX))
        drag = YES;
    else
        self.state = UIGestureRecognizerStateFailed;
}
}

-(void)reset
{
[super reset];
drag = NO;
moveX = 0;
moveY = 0;
}

In my parent view controller, which I believe would be the UITableView in this case, I also implemented the following. I think in your case, you'd want to return no if it's a horizontal pan.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if([gestureRecognizer isKindOfClass:[VerticalPanGestureRecognizer class]])
{
    return YES;
}
return NO;
}

Let me know if any of this is unclear.

Good Luck!