Two UIScrollViews, synchronous scrolling

cocoa-touch, ios, iphone, uikit, uiscrollview

Solution

use the UIScrollViewDelegate Protocol method:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  if (scrollView == UIScrollView1){
    UIScrollView2.contentOffset = scrollView.contentOffset;
  }else{
    UIScrollView1.contentOffset = scrollView.contentOffset;
  }
}

Problem

I heve two UIScrollViews, they are on top of each other. ``` UIView | -------------------------- | | UIScrollView1 UIScrollView2 ``` I would like it, to work in the following way. If I scroll UIScrollView2, UIScrollView1 should also scroll by the same contentOffset. It must by done synchronously, so using `scrollViewDidScroll` is not an option. Do you guys have some idea, how can it be done? Source Code ``` _prContentGridView = [[PRContentGridView alloc] initWithFrame:frame]; _prContentGridView.minimumZoomScale = 0.25; _prContentGridView.maximumZoomScale = 2.0; _prContentGridView.delegate = self; _prBackgroundGridView = [[PRBackgroundGridView alloc] initWithFrame:frame]; [self addSubview:_prBackgroundGridView]; [self addSubview:_prContentGridView]; ``` Delegate Method ``` - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (_prContentGridView.scrollEnabled == YES) { CGPoint p = CGPointMake(scrollView.contentOffset.x - _prevousContentOffsetOfContentScrollView.x, scrollView.contentOffset.y - _prevousContentOffsetOfContentScrollView.y); [_prBackgroundGridView setContentOffset:p animated:YES]; } } ```

Original source