pawelropa
pawelropa

Reputation: 1439

Two UIScrollViews, synchronous scrolling

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

Upvotes: 3

Views: 1957

Answers (2)

Ankit Gupta
Ankit Gupta

Reputation: 958

You should try this Code, first Declare IBOutlet in .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);
  }
}

Upvotes: 2

meronix
meronix

Reputation: 6176

use the UIScrollViewDelegate Protocol method:

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

Upvotes: 8

Related Questions