collection view paging customized

227 Views Asked by At

I used two collection views and they are connected to each other for scrolling. if one scroll the other one will scroll too. this is handling in my didScroll delegate function as below:

  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 {

    }

}

as you see var nesbat is calculating a float which is because my bottomCollection View cell width is - 65 than the sliderCollectionView.

both of them set to paging scroll. the top one (SliderCollectionView) works well. and it is an infinite scrolling which has the duplicate value in my array of data.

so, my question here is when my bottomCollectionview slides, which is a paging scroll, it goes more than its cell width. i want to scroll exactly the width of the cell in bottomCollectionView.

i tested the answers which are saying to handle it on didScroll View but it is ok for my case. I have too many if to handle the infinite scrolling and also the connected collection views.

please help me with the paging scroll to set on the cells width not the width of the whole collectionView. thankx

1

There are 1 best solutions below

0
On

okay i got it myself. i change my top collection view height and move it over the bottom collection view. but the content in xib for collection view bottom constraints changed to + 50 . so i have a collection view on top with its content is - 50 than the height of the collection view and it is over the bottomCollectionView. so i have the suer interaction of my top one .

problem solved :D :)