How to have multiple scrollViews in one scrollView

2019-03-31 09:10发布

问题:

I'd like to have one big scrollview with horizontal scrolling enabled. Within this scrollView I'd like to have (let's say) 5 other scrollviews which can be scrolled vertical.

Can anyone point me in the right direction for how to handle the touchevents?

I'm thinking of making two gesturerecognizer (1 for tap and 1 for pan) and use the delta of the X and Y values for calculating a horizontal or vertical swipe. After I check the direction I set the big scroller or one of the scrollers to enable or disable. Is this the right approuch?


EDIT: Instead of using my method above I was just able to add my 5 scrollviews(vertical scrolling) in one big scrollview(horizontal) by adding the 5 scrollviews as a subview of the big one. Maybe this code can help someone out as well so provided example code as well.

for (int i = 0; i < NumberOfVerticalScrollers; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:frame];
        scroller.directionalLockEnabled = YES;
        scroller.contentSize = CGSizeMake(320, 960);
        [self.scrollView addSubview:scroller];
}
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * NumberOfVerticalScrollers, self.scrollView.frame.size.height);

回答1:

You only need to create a main scrollview which can scroll horizontal, and just need to add other scroll views on main scroll view using addSubView method. iOS handle all the necessary events to handle scrolling properly. You only need to assign correct content size and frame of each scrollview.



回答2:

To do this you will need to simply make 1 horizontal scrollview where you add as subviews the other vertical scroll views. The big thing to handle is when the base scrollview is going to be scrolling horizontally you should disable scrolling on the vertical scrollviews.

You can listen for:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

and when that occurs you can just disable scrolling on your vertical scrollviews

And on the following you enable scrolling back:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate


回答3:

I don't think you'll need to do anything fancy.

see this question

in the example linked to in that question if you disable zooming and bouncing you should see that that it just works.



回答4:

There's an app called "Pulse" that uses a vertical tableview, where each table view cell can be scrolled horizontally and includes another tableview. This takes care of caching and UI events. You need to find an example of that and evaluate it for your purposes (maybe rotating the default implementation by 90 degrees)

How to recreate a Pulse-like UI?