Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have multiple scrollViews in one scrollView

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);
like image 940
BarryK88 Avatar asked Nov 21 '25 11:11

BarryK88


2 Answers

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.

like image 146
rakeshNS Avatar answered Nov 23 '25 23:11

rakeshNS


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
like image 41
rooster117 Avatar answered Nov 23 '25 23:11

rooster117