2

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 :)

Joey
  • 2,912
  • 2
  • 27
  • 32
  • 1
    you can try with TouchesMethods (http://stackoverflow.com/a/7447713/1059705) and http://stackoverflow.com/a/1686087/1059705 and get the angle between the start point and dragged point .. – Bala Mar 27 '13 at 06:20

1 Answers1

2

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.

WolfLink
  • 3,308
  • 2
  • 26
  • 44
  • I tried to solve the problem with those methods overriding UIView and UIScrollView, but I couldn't. I guess I need to create own scrollview and control everything... anyway thank you very much! Cheers! – Joey Apr 04 '13 at 00:48
  • Don't ever EVER create your own scroll view from scratch. The iOS experience is defined by the behavior/physics of the ever powerful UIScrollView. Even if you're good enough to reverse engineer the behavior in one release of iOS, good luck spotting the changes in subsequent releases. – jarjar Aug 14 '13 at 06:02
  • I wasn't suggesting writing a scrollview from scratch. I was thinking something more along the lines of detecting the angle of the swipe based on those functions and then only allowing one of the scrollviews to move. Perhaps that could work by selectively calling `super` in each function or by changing `userInteractionEnabled`. – WolfLink Aug 14 '13 at 19:11