Scroll parent scroll view when child scroll view reaches end
Asked Answered
H

1

9

I have a child scroll view contained within a parent scroll view (not a direct child). What I want is for the parent scroll view to start scrolling in the same direction as soon as the child scroll view reaches the end of its content.

This kind of works out of the box, but not really. Right now I have to lift my finger to make the parent scroll view start scrolling after the child has reached the end.

Any thoughts on this?

EDIT:

An example of what I'm looking for can be seen in Snapchat by swiping right in a table view cell to reveal the chat controller.

Halfhearted answered 12/9, 2015 at 10:2 Comment(1)
Here is a great example repo for this task: github.com/goktugyil/EZSwipeControllerBeaudoin
E
0

Not sure if I correctly understand what does not being a direct child mean. But iOS should do it automatically for you as soon as you reached the end of scroll of child view it should let you scrolling parent scroll.

For this you need to implement for the child:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:   (BOOL)decelerate{
   scrollView.bounces = NO;
}

scrollView.bounces = NO does not let scrollView to jump back on end scroll. So it's just stick at the position it stops scrolling.

And then you need to trigger parent scrolling from the scrollViewDidScroll method for the child.

Something similar to:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        CGFloat scrollViewHeight = scrollView.frame.size.height;
        CGFloat scrollContentSizeHeight = scrollView.contentSize.height;
        CGFloat scrollOffset = scrollView.contentOffset.y;

        CGFloat offset = scrollContentSizeHeight - (scrollOffset + scrollViewHeight);
        if (offset <= 0)
            [self.parentView setContentOffset:CGPointMake(self.parentView.contentOffset.x,  self.parentView.contentOffset.y + diffOffset) animated:YES];
}

Where diffOffset is the distance you want the parentScroll to scroll.

You may prefer more advanced workaround.

For example you can implement

scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset

so you know what is the velocity of the scroll, comparing targetContentOffset to scrollContentSizeHeight and scrollViewHeight should let you know that this particular scroll is about to end dragging reaches the end of scroll view content.

And you can calculate diffOffset and animationTime more accurately.

It could be nice to block scrolling for child view when it's not fully visible within parent scroll, so the scrolling ability appears only when you scroll up until certain position.

Essence answered 12/9, 2015 at 18:25 Comment(2)
Thanks for your reply, I will be sure to try out your solution.Halfhearted
Just in a case, I have created simple test project where put two UITableView's vertically inside UIScrollView and it did work. I can put it somewhere if needed.Essence

© 2022 - 2024 — McMap. All rights reserved.