iOS: 如何同步两个UIScrollview

iOS: How sync two UIScrollview

我有两个水平的 UIScrollview。当用户在其中任何一个中拖动手指时,我想同步它们的滚动。这是我的代码:

self.topScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
self.topScrollView.delegate = self;
self.topScrollView.bounces = YES;

self.bottomScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
self.bottomScrollView.delegate = self;
self.bottomScrollView.bounces = YES;
...

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView == self.topScrollView)
    {
        self.bottomScrollView.delegate = nil;
    }
    else
    {
        self.topScrollView.delegate = nil;
    }
    ...
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{   
    ...
    self.topScrollView.delegate = self;
    self.bottomScrollView.delegate = self;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    ...
    self.topScrollView.delegate = self;
    self.bottomScrollView.delegate = self;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // Sync the two scroll views
    if (scrollView == self.topScrollView)
    {
        [self.bottomScrollView setContentOffset:scrollView.contentOffset animated:NO];
    }
    else
    {
        [self.topScrollView setContentOffset:scrollView.contentOffset animated:NO];
    }
    ...
}

两个滚动视图确实是同步滚动的,但是,问题是所有的弹跳和减速都没有了。整个滚动运动变得非常僵硬。如果我删除所有同步代码,那么每个滚动视图都可以单独工作。 那么,问题是什么?还是可以UIScrollView不同步?

您可以使用 topScrollView.panGestureRecognizerbottomScrollView.panGestureRecognizer 获取两个手势识别器并将它们添加到包含两个滚动视图的公共超级视图。然后 children 会识别此超级视图上的平移手势。

您很可能还需要成为两个识别器的委托并让它们同时被识别:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

我根据jszumski的回答得出了解决方案。但是,我的情况是两个并排的垂直 UIScrollView(scrollViewLeft 和 scrollViewRight)。它应该适用于水平 UIScrollViews 而无需太多修改。

首先,创建一个自定义的 UIScrollView。

//.h file

@interface CSScrollView : UIScrollView

@end

//.m file

@implementation CSScrollView


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    return YES;

}

@end

其次,在您的主视图中,

- (void)viewDidLoad {

    [super viewDidLoad];


    [self.view addGestureRecognizer:self.scrollViewLeft.panGestureRecognizer];

    [self.view addGestureRecognizer:self.scrollViewRight.panGestureRecognizer];

}

这就是设置两个同步滚动视图所需的一切。实际效果比在scrollview的scrollViewDidScroll和同步contentOffset中sending/subscribing通知的常规方式好多了。

@jszumski 在 Swift

中回答
  1. 创建 UIScrollView 的自定义子类
  2. 符合UIGestureRecognizer委托
  3. 覆盖 gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:) GestureRecognizerDelegate 方法
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
  1. 两个集合视图需要有相同的超级视图。将两个手势识别器添加到您的超级视图
yourSuperview.addGestureRecognizer(scrollView1.panGestureRecognizer)
yourSuperview.addGestureRecognizer(scrollView1)

希望对您有所帮助!