简体   繁体   English

在启用分页的同时使两个UICollectionView的滚动同步

[英]Make two UICollectionView's scroll synchronously with paging enabled

Is it possible to make two collectionviews scroll synchronously, and if yes, how? 是否可以使两个collectionview同步滚动,如果可以,如何?

I have this class, containing two collectionviews - one with a date, and another, containing a tableview, with elements matching the date from the first collectionview. 我有一个此类,其中包含两个collectionviews-一个包含日期,另一个包含tableview,其元素与第一个collectionview中的日期匹配。 When i scroll either the date, or the elements, i want them to follow each other. 当我滚动日期或元素时,我希望它们彼此跟随。

Here's some code, and what i've tried so far, but without any luck: 这是一些代码,到目前为止我已经尝试过,但是没有运气:

I added the ScrollViewDelegate to my class, and tried this delegate-method: 我将ScrollViewDelegate添加到我的类中,并尝试了此委托方法:

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.isEqual(dayCollectionView) {
        var offset = contentCollectionView.contentOffset
        offset.x = dayCollectionView.contentOffset.x
        contentCollectionView.setContentOffset(offset, animated: true)
    } else {
        var offset = dayCollectionView.contentOffset
        offset.x = contentCollectionView.contentOffset.x
        dayCollectionView.setContentOffset(offset, animated: true)
    }
}

This method works some how, but the scrollview's are just going mental, and cant seem to stop scrolling, when they're first starts rolling.. 这种方法的工作原理是如何的,但是scrollview只是刚开始,在它们开始滚动时似乎无法停止滚动。

I also tried this: 我也试过这个:

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

    if scrollView.isEqual(dayCollectionView) {
        // Days
        for cell in self.dayCollectionView.visibleCells() {
            if let indexPath = dayCollectionView.indexPathForCell(cell) {
                contentCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
            }
        }
    } else {
        // Classes
        for cell in self.contentCollectionView.visibleCells() {
            if let indexPath = contentCollectionView.indexPathForCell(cell) {
                dayCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
            }
        }
    }
}

which is the best solution so far - but it's still not exactly what I want - this one scrolls, when the other collectionview is done scrolling.. But I want them to scroll synchronously :-) 到目前为止,这是最好的解决方案-但它仍然不是我想要的-当另一个collectionview完成滚动时,这个滚动。.但我希望它们同步滚动:-)

Any suggestions? 有什么建议么?

Indeed, the method scrollViewDidScroll(_:) is the best method for handling such an experience. 实际上,方法scrollViewDidScroll(_:)是处理此类体验的最佳方法。

What seems to be happening here is that the delegate method scrollViewDidScroll gets called for the first CollectionView and when the contentOffset is applied to the other CollectionView, the delegate method gets called again and scroll is applied to the original collectionView. 似乎正在发生的事情是,第一个CollectionView调用了委托方法scrollViewDidScroll,并且当contentOffset应用于另一个CollectionView时,再次调用了该委托方法,并将scroll应用于原始collectionView。 You need to recognize which of the collectionViews is being scrolled at the time. 您需要识别当时正在滚动哪个collectionViews。

For this, you can make use of the isDragging property of UIScrollView. 为此,您可以使用UIScrollView的isDragging属性。

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.isEqual(dayCollectionView), scrollView.isDragging {
        var offset = contentCollectionView.contentOffset
        offset.x = dayCollectionView.contentOffset.x
        contentCollectionView.setContentOffset(offset, animated: true)
    } else if scrollView.isEqual(contentCollectionView), scrollView.isDragging {
        var offset = dayCollectionView.contentOffset
        offset.x = contentCollectionView.contentOffset.x
        dayCollectionView.setContentOffset(offset, animated: true)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM