3

我有两个 UIScrollView,它们在彼此之上。

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

我希望它以下列方式工作。如果我滚动 UIScrollView2,UIScrollView1 也应该滚动相同的 contentOffset。它必须同步完成,因此scrollViewDidScroll不能选择使用。你们有什么想法,怎么做?

源代码

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

委托方法

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

2 回答 2

8

使用 UIScrollViewDelegate 协议方法:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  if (scrollView == UIScrollView1){
    UIScrollView2.contentOffset = scrollView.contentOffset;
  }else{
    UIScrollView1.contentOffset = scrollView.contentOffset;
  }
}
于 2012-04-26T09:04:58.697 回答
2

你应该试试这个代码,首先在 .h 文件中声明 IBOutlet,

IBOutlet UIScrollView *FirstScrollView;
IBOutlet UIScrollView *SecondScrollView;

然后试试这段代码,

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  if ([scrollView isEqual: FirstScrollView])
  {
            SecondScrollView.contentOffset =
              CGPointMake(FirstScrollView.contentOffset.x, 0);
  }
  else
  {
            FirstScrollView.contentOffset = 
              CGPointMake(SecondScrollView.contentOffset.x, 0);
  }
}
于 2012-04-26T09:16:54.733 回答