Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward vertical scrolls from UIScrollView to a sibling UITableView

I have a view controller with this hierarchy:

View Controller:

  • UIScrollView (scrollable horizontally)
  • UITableView (scrollable vertically)

enter image description here

I want to forward the vertical scrolls from my UIScrollView to the sibling UITableView, so that when the user scrolls up on the UIScrollView, the UITableView will scroll up instead. What would be the best way to do it?

I have tried these:

  • Detecting the vertical scroll in scrollViewDidScroll, it doesn't get called because the contentOffset of the scroll view does not change.
  • Subclassing the UIScrollView and overriding touchesMoved, I can't forward the touches to the table view because I don't have a reference to it in this class.
like image 359
Enrico Susatyo Avatar asked Nov 17 '14 07:11

Enrico Susatyo


2 Answers

If the tableview is contained within the scroll view I believe you can set up the scroll view's gesture recognizers to respond only if the table view's gesture recognizers fail. I haven't had a chance to try this, but you should be able to set up a dependency between the gestures for each of the views.

UITableView* tableView = ...;
UIScrollView* scrollView = ...;
for (UIGestureRecognizer* r in scrollView.gestureRecognizers)
{
    for (UIGestureRecognizer* tableRecognizer in tableView.gestureRecognizers)
    {
        [r requireGestureRecognizerToFail:tableRecognizer];
    }
}
like image 131
Stephen Johnson Avatar answered Nov 01 '22 21:11

Stephen Johnson


This will make your scroll simultaneously with UITableView and UIScrollView and apply @Stephen Johnson's block

 - (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer
 shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
 {
     return YES;
 }
like image 23
Sarat Patel Avatar answered Nov 01 '22 20:11

Sarat Patel