0

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

4

1 回答 1

0

好吧,我自己搞定了。我更改了我的顶部集合视图高度并将其移动到底部集合视图上。但集合视图底部约束的 xib 中的内容更改为 + 50 。所以我在顶部有一个集合视图,其内容是 - 比集合视图的高度高 50 并且它位于底部集合视图之上。所以我有我最喜欢的人的互动。

问题解决了:D :)

于 2015-08-12T11:58:43.643 回答