Two UIScrollViews, synchronous scrolling

I see two UIScrollViews, they are on top of each other.

                                        UIView
                                           |
                              --------------------------
                              |                        |
                         UIScrollView1            UIScrollView2

I would like it to work as follows. If I scroll UIScrollView2, UIScrollView1 should also scroll with the same contentOffset. This must be done synchronously, so use is scrollViewDidScrollnot an option. Do you have an idea how to do this?

Source

    _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];

Delegation 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];
    }
}
+3
source share
2 answers

use the protocol method UIScrollViewDelegate:

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

You should try this code, first declare IBOutlet in the .h file,

IBOutlet UIScrollView *FirstScrollView;
IBOutlet UIScrollView *SecondScrollView;

then try this code,

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  if ([scrollView isEqual: FirstScrollView])
  {
            SecondScrollView.contentOffset =
              CGPointMake(FirstScrollView.contentOffset.x, 0);
  }
  else
  {
            FirstScrollView.contentOffset = 
              CGPointMake(SecondScrollView.contentOffset.x, 0);
  }
}
+2
source

All Articles