【问题标题】:Making two UIScrollViews follow each others scrolling使两个 UIScrollViews 跟随彼此滚动
【发布时间】:2011-12-16 15:17:41
【问题描述】:

如何让两个滚动视图跟随彼此滚动?

例如,我在屏幕左侧有一个滚动视图 (A),其内容可以上下滚动,但不能左右滚动。滚动视图 B 匹配 A 的上下滚动,但也可以左右滚动。滚动视图 A 始终在屏幕上。

-----------------------------------------------------------
|             |                                           |
|             |                                           |
|             |                                           |
|     A       |                    B                      |
|             |                                           |
|    scrolls  |                                           |
|   up & down |              scrolls all directions       |
|             |                                           |
-----------------------------------------------------------

我将如何做到这一点,以便(任一视图的)上下滚动也会使另一个视图沿相同的上下方向滚动?还是有其他方法可以做到这一点?

【问题讨论】:

    标签: iphone objective-c ios uiscrollview


    【解决方案1】:

    将滚动视图 A 的委托设置为您的视图控制器...然后拥有...

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
      CGPoint offset = scrollViewB.contentOffset;
      offset.y = scrollViewA.contentOffset.y;
      [scrollViewB setContentOffset:offset];
    }
    

    如果您希望双方互相关注,则为他们设置委托并使用...

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
      if([scrollView isEqual:scrollViewA]) {
        CGPoint offset = scrollViewB.contentOffset;
        offset.y = scrollViewA.contentOffset.y;
        [scrollViewB setContentOffset:offset];
      } else {
        CGPoint offset = scrollViewA.contentOffset;
        offset.y = scrollViewB.contentOffset.y;
        [scrollViewA setContentOffset:offset];
      }
    }
    

    上面可以重构为有一个方法,它接受两个滚动视图并匹配另一个。

    - (void)matchScrollView:(UIScrollView *)first toScrollView:(UIScrollView *)second {
      CGPoint offset = first.contentOffset;
      offset.y = second.contentOffset.y;
      [first setContentOffset:offset];
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
      if([scrollView isEqual:scrollViewA]) {
        [self matchScrollView:scrollViewB toScrollView:scrollViewA];  
      } else {
        [self matchScrollView:scrollViewA toScrollView:scrollViewB];  
      }
    }
    

    Swift 3 版本:

        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if scrollView == scrollViewA {
                self.synchronizeScrollView(scrollViewB, toScrollView: scrollViewA)
            }
            else if scrollView == scrollViewB {
                self.synchronizeScrollView(scrollViewA, toScrollView: scrollViewB)
            }
        }
    
        func synchronizeScrollView(_ scrollViewToScroll: UIScrollView, toScrollView scrolledView: UIScrollView) {
            var offset = scrollViewToScroll.contentOffset
            offset.y = scrolledView.contentOffset.y
    
            scrollViewToScroll.setContentOffset(offset, animated: false)
        }
    

    【讨论】:

    • 你能建议我以不同的速度滚动
    【解决方案2】:

    我在 iOS 11 上尝试了 Simon Lee 的答案。它有效但不是很好。两个滚动视图是同步的,但是使用他的方法,滚动视图会失去惯性效果(松开手指后继续滚动)和弹跳效果。我认为这是因为通过setContentOffset(offset, animated: false) 方法设置contentOffset 会导致scrollViewDidScroll(_ scrollView: UIScrollView) 委托方法的循环调用(参见this question)

    这是适用于 iOS 11 的解决方案:

     // implement UIScrollViewDelegate method
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if scrollView == self.scrollViewA {
                self.syncScrollView(self.scrollViewB, toScrollView: self.scrollViewA)
            }
            else if scrollView == self.scrollViewB {
                self.syncScrollView(self.scrollViewA, toScrollView: scrollViewB)
            }
        }
    
        func syncScrollView(_ scrollViewToScroll: UIScrollView, toScrollView scrolledView: UIScrollView) {
            var scrollBounds = scrollViewToScroll.bounds
            scrollBounds.origin.y = scrolledView.contentOffset.y
            scrollViewToScroll.bounds = scrollBounds
        }
    

    因此,我们没有设置contentOffset,而是使用bounds 属性将另一个滚动视图与用户滚动的滚动视图同步。这样,委托方法scrollViewDidScroll(_ scrollView: UIScrollView) 就不会被循环调用,并且滚动非常平滑,并且具有与单个滚动视图一样的惯性和弹跳效果。

    【讨论】:

    • 我刚刚尝试了您的解决方案,但 scrollBounds 始终是相同的值。你在提供的代码之外做了什么不同的事情吗?
    • 没有。与两个滚动视图的滚动同步相关,这是我项目中的所有内容,并且工作正常。
    • 这是 iOS 11+ 的正确答案。正如 Dmitry 所说,以前的解决方案会循环调用scrollViewDidScroll,这会导致奇怪的行为。设置视图的边界不会调用 scrollViewDidScroll。
    • scrollBounds.origin.x = scrolledView.contentOffset.x ,如果你使用水平滚动视图
    【解决方案3】:

    Swift 5.4 // Xcode 13.1

    对我来说完美无缺的是:

    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)
    

    希望这会有所帮助!

    【讨论】:

      【解决方案4】:

      伙计们,我知道问题已得到解答,但决定在这里与您分享我解决类似问题的方法,因为我相信这是一个非常干净的解决方案。基本上我必须让三个集合视图一起滚动,我所做的是我做了一个custom UICollectionView subclass called SharedOffsetCollectionView,当你将这个类设置为故事板中的集合视图或直接从代码实例化它时,你可以让所有实例滚动一样的。

      因此,使用 SharedOffsetCollectionView 应用程序中此类的所有集合视图实例,将始终滚动相同。在我看来,这是一个干净的解决方案,因为它需要在您的视图控制器中添加零逻辑,它都包含在这个外部类中,您只需将集合视图的类设置为 SharedOffsetCollectionView 就可以了。

      同样的方法可以很容易地转移到UITableViews 和UIScrollViews

      希望对您有所帮助。 :)

      我的解决方案写在:

      Swift 5.2,XCode 11.4.1

      【讨论】:

        【解决方案5】:

        最重要的答案对我来说不太适用,因为我遇到了scrollViewDidScroll 的循环调用。发生这种情况是因为设置滚动视图的内容偏移量也会调用scrollViewDidScoll。我通过在它之间放置一个锁来解决它,它是根据用户是否拖动滚动视图来设置的,因此通过设置内容偏移量不会发生同步:

        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            self.synchronizeScrollView(offSet: scrollView.contentOffset.y,
                                       scrollViewAIsScrolling: scrollView == scrollViewA,
                                       isScroller: scrollView.isDragging)
        }
        
        private enum Scroller {
            case page, time
        }
        
        private var scroller: Scroller? = nil
        
        func synchronizeScrollView(offSet: CGFloat, scrollViewAIsScrolling: Bool,
                                   isScroller: scrollView.isDragging) {
            
            let scrollViewToScroll = scrollViewAIsScrolling ? scrollViewB : scrollViewA
            var offset = scrollViewToScroll.contentOffset
            offset.y = offSet
            scrollViewToScroll.setContentOffset(offset, animated: false)
        }
        

        可以根据使用的滚动视图数量和拥有者来重构此代码。我不建议让一个控制器成为许多滚动视图的代表。我宁愿通过委托来解决它。

        【讨论】:

          猜你喜欢
          • 2016-09-16
          • 1970-01-01
          • 1970-01-01
          • 2022-01-16
          • 1970-01-01
          • 2014-11-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多