Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
    }
}
like image 806
pawelropa Avatar asked Feb 18 '26 01:02

pawelropa


2 Answers

use the UIScrollViewDelegate Protocol method:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  if (scrollView == UIScrollView1){
    UIScrollView2.contentOffset = scrollView.contentOffset;
  }else{
    UIScrollView1.contentOffset = scrollView.contentOffset;
  }
}
like image 109
meronix Avatar answered Feb 19 '26 13:02

meronix


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);
  }
}
like image 42
Ankit Gupta Avatar answered Feb 19 '26 14:02

Ankit Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!