Manually scrolling two UICollectionViews showing the same content

本小妞迷上赌 提交于 2019-12-02 04:24:40

Keep track of the currently dragging scroll view when scrollViewWillBeginDragging: is called.

In scrollViewDidScroll:, update the scroll view that is not dragging:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView == self.mainCollectionView 
             && self.mainCollectionView == self.scrollingView){ // new check
        CGFloat x = self.mainCollectionView.contentOffset.x / self.mainCollectionView.bounds.size.width * SM_IPHONE_THUMB_CONTAINER_SIZE; // cell width + spacing 48 + 8
        CGFloat y = 0;
        CGPoint contentOffset = CGPointMake(x, y);
        self.thumbsCollectionView.contentOffset = contentOffset;

    }
    else if(scrollView == self.thumbsCollectionView 
           && self.thumbsCollectionView== self.scrollingView){ // new check
        CGFloat   x = self.thumbsCollectionView.contentOffset.x / self.thumbsCollectionView.bounds.size.width * SM_IPHONE_THUMB_CONTAINER_SIZE; // cell width + spacing 48 + 8
        CGFloat y = 0;
        CGPoint contentOffset = CGPointMake(x, y);
        self.mainCollectionView.contentOffset = contentOffset;

}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!