ios - 如何将平移手势从 ScrollView 转发到另一个 ScrollView

标签 ios uiscrollview uigesturerecognizer

我有两个 UIScrollView,如果用户在 ScrollView 中滚动,我想让另一个 ScrollView 也滚动。我已经阅读了一个解决方案,涉及将 pangesturerecognizer 从最初平移的 ScrollView 传递到另一个 ScrollView ,但如果我这样做,原始 ScrollView 根本不会滚动。

我发现,有一个委托(delegate)方法

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

但是如果我尝试连接 ScrollView 的 pangesturerecognizers 的委托(delegate),我的应用程序就会崩溃。

希望有人能帮助我。

最佳答案

你只需要更新第二个 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 委托(delegate)并实现相同的委托(delegate)。

    - (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。

关于ios - 如何将平移手势从 ScrollView 转发到另一个 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15408204/

相关文章:

ios - UIPanGestureRecognizer 在触摸时立即做一些事情

ios - NSJSONSerialization 产生垃圾

ios - 删除 UIView 后重新定位的问题

ios - MutableDictionary 变为不可变

ios - UIScrollView的zoomScale不一致

ios - 滚动到 UITextView 文本的末尾

ios - 在占位符 UIView 上使用手势识别器平移 UIImage

iPhone在哪些情况下无法获取mac地址成功?

ios - UITableView滚动执行动画CPU使用率高

ios - 点击自定义注释并执行相应的功能