0
votes

I have 2 scrollviews, the smaller scrollview needs to scroll a little bit slower (and stop on the next "page) than the larger scrollview. So basically, scrolling the larger scrollview scrolls the smaller scrollview, but at a slower pace than the larger scrollview. (confusing I know).

So scrollView1 (the larger) and scrollView2, the smaller: As you swipe scrollView1, scrollView2 is also scrolling but slower. Both having Paging enabled and their contentSizes have already been set based on scrollView2's content.

I am just having trouble calculating the offset between the 2 so they scroll perfectly.

enter image description here

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ( scrollView == scrollView1 )
    {
        CGFloat xOffset = (scrollView2.contentSize.width * scrollView1.contentSize.width); // the issue

        [scrollView2 scrollRectToVisible:CGRectMake(xOffset, 0, scrollView2.frame.size.width, scrollView2.frame.size.height) animated:YES];
    }
}
2

2 Answers

1
votes

Try replacing those two lines with:

float xOffset = scrollView1.contentOffset.x * (scrollView2.frame.size.width / scrollView1.frame.size.width);
[scrollView2 setContentOffSet:CGPointMake(xOffset,0) animated:YES];

This will take the offset of scrollView1, divide it by the difference in frame sizes between the two views, and set scrollView2's contentOffset to that value (which is better than scrolling rect to visible).

1
votes

this worked for me try it..

CGPoint offset = CGPointMake(scroll1.contentOffset.x, scroll1.contentOffset.y);

offset.x /= 3;
offset.y /= 3;

// Scroll the background scroll view by some smaller offset
scroll2.contentOffset = offset;