我使用了两个集合视图,它们相互连接以进行滚动。如果一个滚动另一个也将滚动。这是在我的 didScroll 委托函数中处理的,如下所示:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var screenHeight = max(Int(UIScreen.mainScreen().bounds.width), Int(UIScreen.mainScreen().bounds.height))
if ( collectionView == self.bottomSliderCollectionView)
{
return CGSizeMake(self.sliderCollectionView.frame.width - 65, 50)
}else {
return CGSizeMake(self.sliderCollectionView.frame.width , self.sliderCollectionView.frame.height)
}
}
func scrollViewDidScroll(scrollView: UIScrollView) {
if (scrollView == self.sliderCollectionView ){
// Calculate where the collection view should be at the right-hand end item
var contentOffsetWhenFullyScrolledRight = self.sliderCollectionView.frame.size.width * CGFloat(self.workingMenuSlider.count - 1)
if (scrollView.contentOffset.x == contentOffsetWhenFullyScrolledRight ) {
// user is scrolling to the right from the last item to the 'fake' item 1.
// reposition offset to show the 'real' item 1 at the left-hand end of the collection view
var newIndexPath = NSIndexPath(forItem: 1, inSection: 0)
self.sliderCollectionView.scrollToItemAtIndexPath(newIndexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
} else if (scrollView.contentOffset.x == 0) {
// user is scrolling to the left from the first item to the fake 'item N'.
// reposition offset to show the 'real' item N at the right end end of the collection view
var newIndexPath = NSIndexPath(forItem: self.workingMenuSlider.count - 2, inSection: 0)
self.sliderCollectionView.scrollToItemAtIndexPath(newIndexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
}
}
if (scrollView.contentOffset.x < 0) {
var newIndexPath = NSIndexPath(forItem: self.workingMenuSlider.count - 2, inSection: 0)
self.sliderCollectionView.scrollToItemAtIndexPath(newIndexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
self.bottomSliderCollectionView.scrollToItemAtIndexPath(newIndexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
}
var contentOffsetWhenFullyScrolledRight = self.sliderCollectionView.frame.size.width * CGFloat(self.workingMenuSlider.count - 1)
if (scrollView.contentOffset.x > contentOffsetWhenFullyScrolledRight) {
var newIndexPath = NSIndexPath(forItem: 1, inSection: 0)
self.sliderCollectionView.scrollToItemAtIndexPath(newIndexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
self.bottomSliderCollectionView.scrollToItemAtIndexPath(newIndexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
}
if (self.sepratedScroll == true ) {
if (scrollView == self.sliderCollectionView ){
var nesbat = (self.bottomSliderCollectionView.frame.width - 65) / self.sliderCollectionView.frame.width
self.sepratedScroll = false
self.bottomSliderCollectionView.contentOffset.x = self.sliderCollectionView.contentOffset.x * nesbat
self.sepratedScroll = true
}else {
self.sepratedScroll = false
var nesbat = (self.bottomSliderCollectionView.frame.width - 65) / self.sliderCollectionView.frame.width
self.sliderCollectionView.contentOffset.x = (self.bottomSliderCollectionView.contentOffset.x ) / nesbat
self.sepratedScroll = true
}
} else {
}
}
如您所见,var nesbat 正在计算一个浮点数,这是因为我的 bottomCollection View 单元格宽度是 - 65,而不是 sliderCollectionView。
他们都设置为分页滚动。顶部的(SliderCollectionView)效果很好。这是一个无限滚动,在我的数据数组中具有重复值。
所以,我的问题是当我的bottomCollectionview 滑动时,它是一个分页滚动,它超出了它的单元格宽度。我想准确滚动bottomCollectionView中单元格的宽度。
我测试了在 didScroll View 上处理它的答案,但对我来说没问题。如果要处理无限滚动以及连接的集合视图,我有太多的东西。
请帮助我将分页滚动设置为单元格宽度而不是整个 collectionView 的宽度。谢谢