Conflictive scroll on UITableView within UIScrollV

2019-08-15 06:15发布

问题:

I had three UITableViews contained in one UIScrollView(pagingEnable = YES),

The hierarchy looks like:

                                 +--visible area--+  ---+
+---------`UIScrollView`---------+---------------+|  --+|
| +-------------+ +-------------+|+-------------+||  -+||
| |      0      | |      1      |||      2      |||   |||
| |`UITableView`| |`UITableView`|||`UITableView`|||  equal height
| |             | |             |||             |||   |||
| +-------------+ +-------------+|+-------------+||  -+||
+--------------------------------+---------------+|  --+| 
                                 +----------------+  ---+

The UIScrollView (as container view) had it'scontentSize.height set to fit the screen height, thus it's only scrollable in horizontal direction. And each UITableView is vertically scrollable as is.

The problem is I have no idea which view (ScrollView or TableView) handle a current finger touch on the screen.

It works fine when I scroll a perfect vertical / horizontal path through the screen to move the ScrollView or TableView.

While if I scroll from the bottom right to the top left (like a backslash "\") it sometimes move the ScrollView horizontally, sometimes move the TableView vertically...even worse it move none of both but just shake the TableView ridiculously. I just can't move what I expected.

I assume it's because UIScrollView and UITableView's ScrollView have detection conflict .

Here I added log to the UIScrollViewDelegate method:

//in each ViewController handle a UITableView

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
            NSLog(@"TableView touched")
        }
    }

//in the ViewController handle the UIScrollView

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
            NSLog(@"ScrollView touched")
        }
    }

//as a side note...I set three ViewControllers(each holds one TableView) into a ContainerViewController(holds ScrollView)

They logged out one after another randomly...but almost the same time.

Any idea would be appreciated!

回答1:

Add a UIPanGestureRecognizer for the entire view. Store the initial location, and from the current location, calculate if it is a horizontal or vertical movement. (If the x coordinate is changing more, than it is horizontal, if y, vertical.)

Once the direction is clear, forward the movement to the table view or the scroll view.

Or a better solution: use a table view controller and add edge swipe gestures to switch like if they were in a tab controller.