3

guys! I have a UIScrollView (Main Scroll view) which I would like to be scrolled only vertically. Inside it I have another UIScrollView (child scroll view) which should scroll only horizontally. In the child Scroll View I have two views. Here is a picture to illustrate that. My problem is that the child scroll view doesn't scroll horizontally.

I use Auto layout but also tried with:

[self.innerScrollView setDelegate:self];
[self.innerScrollView setScrollEnabled:YES];
self.innerScrollView.pagingEnabled = YES;
self.innerScrollView.contentSize = CGSizeMake(640, 300);

I also tried with subclassing both scroll view's from UIScrollView and using:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer: (UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

I'm a bit clueless at this point, so any input would be greatly appreciated.

enter image description here

Indrajeet
  • 5,490
  • 2
  • 28
  • 43
Milen
  • 81
  • 2
  • 8
  • Is this what you're after ? http://stackoverflow.com/questions/23008843/the-uicollectionview-swipe-away-in-ios7-app-manager be sure to tick-up Mehan's great answer! – Fattie May 24 '14 at 11:56

2 Answers2

4

Make your inner scrollView frame width 320

To make your scrollView scrollable horizontal make the contentSize width bigger than it's frame width

Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
  • My inner scroll view has width 640, because its subviews both have width 320 and one starts from 0, 0, and another from 320, 0. So in the inner scroll view I would like to move between these two views on scrolling horizontally. Main Scroll View shouldn't scroll horizontally as showed on the picture. – Milen May 24 '14 at 12:06
  • Sir u need `contentSize.width` 640. u have that code. Make ur `self.innerScrollView.frame.size.width` 320 everything should work fine – Warif Akhand Rishi May 24 '14 at 12:11
  • Warif Akhand Rishi, finally I understand you and that really fixed my problem!!! Thank you so much!!! – Milen May 24 '14 at 12:29
0

On the newer iOS I have to implement manual logic to achieve this.

If I want vertical scrolling on the parent scrollview while I am horizontally scrolling the child scrollview which is nested within it, I have to enabled the UIScrollView delegate to my current class on the child scrollview, then use the following logic I created:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView == childScrollView) {
        static float lastOffsetY;
        float currentOffsetY = [scrollView.panGestureRecognizer translationInView:scrollView.superview].y;
        if (scrollView.panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
            lastOffsetY = currentOffsetY;
        } else {
            float dy = currentOffsetY-lastOffsetY;
            [UIView animateWithDuration:0.1f
                                  delay:0.0f
                                options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState)
                             animations:^(void) {
                                 [parentScrollView setContentOffset:CGPointMake(parentScrollView.contentOffset.x,
                                                                          parentScrollView.contentOffset.y-dy)
                                                     animated:NO];
                             }
                             completion:^(BOOL finished) {

                             }];
        }
        lastOffsetY = currentOffsetY;
    }

}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

    if (scrollView == childScrollView) {
        float oy = parentScrollView.contentOffset.y;
        float noy = oy;
        if (oy < 0) {
            noy = 0;
        }
        if (oy > parentScrollView.contentSize.height-parentScrollView.frame.size.height) {
            noy = parentScrollView.contentSize.height-parentScrollView.frame.size.height;
        }
        if (noy != oy) {
            [UIView animateWithDuration:0.1f
                                  delay:0.0f
                                options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState)
                             animations:^(void) {
                                 [parentScrollView setContentOffset:CGPointMake(parentScrollView.contentOffset.x,
                                                                          noy)
                                                     animated:NO];
                             }
                             completion:^(BOOL finished) {

                             }];
        }
    }

}
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195