zepojo / UPCarouselFlowLayout

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

current page not get correct #66

Open aishwarya0786 opened 4 years ago

aishwarya0786 commented 4 years ago

I am customizing the frame size then the value of current page is getting wrong.

richardwei6 commented 3 years ago

This worked for me

    // UIScrollView Delegate

    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        // https://stackoverflow.com/a/38312063/

        let centerPoint = CGPoint(x: scrollView.contentOffset.x + (scrollView.frame.width / 2), y: (scrollView.frame.height / 2));
        guard let indexPath = collectionView.indexPathForItem(at: centerPoint) else {
            return;
        }
        print("page - \(indexPath.row)");
    }

Here's the stack overflow post.

alisherFsociety commented 2 years ago
extension UICollectionView {
    func getCurrentIndex() ->IndexPath? {
        let centerPoint = CGPoint(x: self.contentOffset.x + (self.frame.width / 2), y: (self.frame.height / 2));
        return self.indexPathForItem(at: centerPoint)
    }
}