Andy Jacobs
Andy Jacobs

Reputation: 15245

Passthrough touches of 2 UIScrollViews

I have 2 descendants of UIScrollView

I have a UITableView which displays data and i have a UICollectionView added above the UITableView

view  
 | - UITableView
 | - UICollectionView

The UITableView can only scroll vertically and the UICollectionView can only scroll horizontally. I can only scroll my tableview where the collectionview isn't overlapping (which is off course expected behaviour) but i need to make it so that i can scroll my tableview even if i swipe vertically on my collectionview.

I cannot simply add the collectionview as a subview of the tableview because of other reasons (which i know, would make this work)

Is there any other possibility to let de touches from the collectionview passthrough to the tableview?

Upvotes: 9

Views: 1051

Answers (3)

Alexander Tkachenko
Alexander Tkachenko

Reputation: 3281

As I understood, you want touches to be intercepted by UITableView as well as UICollectionView?

I think You can try resending touch events from your UICollectionView to UITableView. (manually calling touchesBegin, touchesMoved, touchesEnded, etc.)

Maybe overriding touchesBegan, touchesMoved, touchesEnded methods will work for your case.

You can try overriding UICollectionView with your subclass (with property set to your UITableView instance) and implementing touch handling methods with something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    if (CGRectContainsPoint(self.tableView.frame, [touch locationInView:self.tableView.superview]) {
       [self.tableView touchesBegan:touches withEvent:event];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesMoved:touches withEvent:event];

        [self.tableView touchesMoved:touches withEvent:event];
    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesEnded:touches withEvent:event];

        [self.tableView touchesEnded:touches withEvent:event];
    }

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesCancelled:touches withEvent:event];

        [self.tableView touchesCancelled:touches withEvent:event];
    }

Hope it will help, however I'm not 100% sure about it.

I've found this article also, maybe it will be useful

http://atastypixel.com/blog/a-trick-for-capturing-all-touch-input-for-the-duration-of-a-touch/

Upvotes: 4

Wei
Wei

Reputation: 3482

You can try to create a subclass of UICollectionView and add this code to your CustomCollectionView's .m file.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];
    if (hitView == self) {
        return nil;
    } else {
        return hitView;
    }
}

Upvotes: 4

Apurv
Apurv

Reputation: 17186

You can add pan gesture recognizer with direction vertical on collectionview. On the vertical pan event, you can change the content offset of your table view to scroll it.

Upvotes: 1

Related Questions