ios - Collection View 分页定制

标签 ios swift collectionview scroll-paging

我使用了两个 Collection View ,它们相互连接以进行滚动。如果一个滚动,另一个也会滚动。 这是在我的 didScroll 委托(delegate)函数中处理的,如下所示:

  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 正在计算 float ,这是因为我的 BottomCollection View 单元格宽度比 sliderCollectionView 宽 - 65。

两者都设置为分页滚动。最上面的一个(SliderCollectionView)效果很好。这是一个无限滚动,在我的数据数组中具有重复值。

所以,我的问题是当我的bottomCollectionview滑动时,它是一个分页滚动,它超出了它的单元格宽度。 我想精确滚动bottomCollectionView 中单元格的宽度。

我测试了答案,这些答案说要在 didScroll View 上处理它,但对于我的情况来说是可以的。我有太多的 if 来处理无限滚动以及连接的 Collection View 。

请帮助我设置分页滚动以设置单元格宽度而不是整个collectionView的宽度。 谢谢

最佳答案

好吧,我自己拿到了。 我更改顶部 Collection View 的高度并将其移动到底部 Collection View 上。但 xib 中 Collection View 底部约束的内容更改为 + 50 。 所以我在顶部有一个 Collection View ,其内容是 - 50 比 Collection View 的高度,并且它位于底部CollectionView之上。 所以我和我的顶级人物有很好的互动。

问题已解决:D:)

关于ios - Collection View 分页定制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31962291/

相关文章:

ios - 在iTunes Connect中添加新的应用内购买时,“保存”按钮消失

iOS - 带有透明视频的水印视频

iphone - 基于 NSStrings 数组从对象的 NSArray 中提取字符串

swift - NSDate startOfDay 是凌晨 4 点?

swift - 理解 RealmSwift 中的平等

ios - 使用 HitTest :withEvent: to return custom object

ios - 为显示错误图像的 MKMapView 设置 annotationView 图像

ios - 在 View Controller 的单个 Collection View 中显示多个图表,如条形图、饼图、折线图

ios - cellForItemAtIndexPath : is not called when using custom UICollectionViewLayout subclass

ios - CollectionViewCell 中的PrepareForReuse