2
votes

Hi, guys!

I have a question about nested scroll view.

There is a scroll view containing nested scroll views. I will just call outer-scrollview and inner-scrollview. outer-scrollview is horizontal scrollview, and inner-scrollviews are vertical-scrollview.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _outerScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _outerScrollView.pagingEnabled = YES;
    _outerScrollView.contentSize = CGSizeMake(_outerScrollView.frame.size.width * 3, _outerScrollView.frame.size.height);
    [self.view addSubview:_outerScrollView];

    for(int i=0; i<3; i++) {

        UIScrollView *innerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i * _outerScrollView.frame.size.width,
                                                                                             0,
                                                                                             _outerScrollView.frame.size.width,
                                                                                             _outerScrollView.frame.size.height)];
        [_outerScrollView addSubview:innerScrollView];


        UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, innerScrollView.frame.size.width, innerScrollView.frame.size.height * 2.0)];
        contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imagefile"]];
        [innerScrollView addSubview:contentView];

        innerScrollView.contentSize = contentView.frame.size;

    }
}

Basically, if only 1 scroll view will be scrolled at the same time.

If I scroll to left or right, then outer-scrollview will be scrolled.

If I scroll to top or bottom, then inner-scrollview will be scrolled,

AND, if I scroll diagonally, one of both will be scrolled. It is depend on the direction of the scrolling.

enter image description here

If the angle is 0~45 degree, inner-scrollview will be scrolled. If the angle is 45~90 degree, outer-scrollview will be scrolled.

Is it possible to change the ANGLE?

For example, even though the angle is 30 degree, I want to scroll horizontally.

Thank you!

Any help would be appreciated :)

1
you can try with TouchesMethods (stackoverflow.com/a/7447713/1059705) and stackoverflow.com/a/1686087/1059705 and get the angle between the start point and dragged point .. - Bala

1 Answers

2
votes

If you want a simple way to achieve this kind of nested scrollview action, you could try simply disabling horizontal scrolling for the inner views and disabling vertical scrolling for the outer view. However, if that does not give you the result you are looking for, you will need to do a lot of custom coding.

First of all, you will need to start your own subclass of UIScrollView. Start by overwriting these functions:

touchesBegan:
touchesMoved:
touchesEnded:
touchesCancelled:

and also

touchesShouldBegin:

From there, you will have to do your own calculations to determine the angle of the drag and send messages to the scrollviews accordingly, possibly be calling super on one or more of the above functions. You might also consider just writing your own scrollview from scratch, you could simply have a UIView as a subview of your subclass.