Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 268
Andy Jacobs Avatar asked May 12 '14 10:05

Andy Jacobs


2 Answers

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;
    }
}
like image 84
Wei Avatar answered Oct 05 '22 23:10

Wei


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/

like image 41
Alexander Tkachenko Avatar answered Oct 06 '22 00:10

Alexander Tkachenko