zepojo / UPCarouselFlowLayout

A fancy carousel flow layout for UICollectionView on iOS.
MIT License
1.66k stars 236 forks source link

Connect carousel to pageControl #30

Closed Bertges closed 6 years ago

Bertges commented 6 years ago

Hi, I'm facing the same problem specified in this issue:

https://github.com/ink-spot/UPCarouselFlowLayout/issues/23

In his case, he's using another framework, but I want to keep using UPCarouselFlowLayout.

There is an easy way to get the current page?

Bertges commented 6 years ago

Solved my problem by getting the indexPath of the center colectionView cell.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let center = CGPoint(x: scrollView.contentOffset.x + (scrollView.frame.width / 2), y: (scrollView.frame.height / 2))
        if let ip = self.collectionView!.indexPathForItem(at: center) {
            self.pageControl.currentPage = ip.row
        }
    }