如何将平移手势从滚动视图转发到另一个滚动视图

时间:2013-03-14 11:39:05

标签: ios uiscrollview uigesturerecognizer

我有两个UIScrollViews,我想让其他滚动视图滚动,如果用户滚动滚动视图。我已经阅读了一个解决方案,涉及将pangesturerecognizer从原始平移的scrollview传递到另一个scrollview但是如果我这样做,原始的scrollview根本不会滚动。

我发现,有一个委托方法

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

但如果我尝试连接滚动视图的pangesturerecognizers的委托我的应用程序崩溃。

希望有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

您只需要更新第二个ScrollView的边界

类似的东西: -

CGRect updateTheViewWithBounds = viewToUpdate.bounds;
updateTheViewWithBounds.origin = scrolledView.contentOffset;
viewToUpdate.bounds = updateTheViewWithBounds;

这将成功。

从评论中可以看出,我将举一个小例子。

在UiViewController上创建两个scrollView

scrollOne = [[UIScrollView alloc] init];
    [scrollOne setBackgroundColor:[UIColor greenColor]];
    [scrollOne setFrame:CGRectMake(10, 20, 200, 300)];
    [scrollOne setContentSize:CGSizeMake(600, 600)];
    scrollOne.delegate = self;

    scrollTwo = [[UIScrollView alloc] init];
    [scrollTwo setBackgroundColor:[UIColor redColor]];
    [scrollTwo setFrame:CGRectMake(230, 20, 200, 300)];
    [scrollTwo setContentSize:CGSizeMake(600, 600)];
    scrollTwo.delegate = self;


    [self.view addSubview:scrollOne];
    [self.view addSubview:scrollTwo];

遵守UIScrollView代理并实现相同的目标。

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if([scrollView isEqual:scrollOne])
    {
        CGRect updateTheViewWithBounds = scrollOne.bounds;
        updateTheViewWithBounds.origin = scrollOne.contentOffset;
        scrollTwo.bounds = updateTheViewWithBounds;
        [scrollTwo flashScrollIndicators];
    }
    else if([scrollView isEqual:scrollTwo])
    {
        CGRect updateTheViewWithBounds = scrollTwo.bounds;
        updateTheViewWithBounds.origin = scrollTwo.contentOffset;
        scrollOne.bounds = updateTheViewWithBounds;
        [scrollOne flashScrollIndicators];
    }
}

滚动上述任何一个scrollView都会滚动两个scrollView。