Manually scrolling two UICollectionViews showing the same content

前端 未结 2 1261
野性不改
野性不改 2021-01-26 20:40

As the title suggest, I have a UIViewController with two UICollectionViews which are displaying the same content in a horizontal fashion. The main one

2条回答
  •  余生分开走
    2021-01-26 20:58

    Use delegates. Subclass the CollectionView and implement the scrollViewDelegate's selector scrollViewDidScroll:. Also create a new property called

    id scrollDistanceDelegate;
    

    Now create your own protocol in the subclasses CollectionView. This protocol will be called when a scroll view is scrolled and will send the distance it was scrolled. So the protocol selector could be:

    scrollView: (UIScrollView *) sv didScrollADistance: (CGFloat) distance
    

    So now in the scrollViewDidScroll: selector, when ever the scrollview scrolls, it would calc the distance. Then call the scrollView:didScrollDistance: method of the scrollDistanceDelegate.

    At this point, for the top CollectionView's scrollDistanceDelegate would be set to the bottom CollectionView, and the bottom CollectionViews' scrollDistancedelgate would be set to the top CollectionView.

    So now when ever a CollectionView scrolls, the other would scroll. the only problem I see is a feed back loop. One scrolls, the other one scrolls, which tells the first one to scroll....

    But that should be able to be dealt with.

提交回复
热议问题