集合视图分页定制

时间:2015-08-12 10:15:17

标签: ios swift collectionview scroll-paging

我使用了两个集合视图,它们相互连接以进行滚动。如果一个滚动另一个滚动也会滚动。 这是我的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的宽度。 thankx

1 个答案:

答案 0 :(得分:0)

好吧,我自己拿到了。 我更改我的顶部集合视图高度并将其移动到底部集合视图上。但是xib中用于集合视图底部约束的内容更改为+ 50。 所以我有一个顶部的集合视图,其内容是 - 50比集合视图的高度,它是在bottomCollectionView上。 所以我有我的顶级互动。

问题解决了:D:)