Ahsan Ebrahim Ahsan Ebrahim - 2 months ago 30x
iOS Question

Detect page change in UICollectionView

I tried finding this question for a while but could not find this problem's answer.
My problem is that i have a

and the Scroll Direction is
Paging Enabled
. My problem is that i want to keep the tack of the current page number on which the user is, so i created an
variable and now want to add or subtract it by 1 each time the user swipes right or left. I tried using
's delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

but when the user swipes right or left, it is called as many number of the times as the number of columns on a page in the
plus it wont let me know that whether the user went to the next page or the previous one.


Use :

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    CGFloat pageWidth = collectionView.frame.size.width;
    float currentPage = collectionView.contentOffset.x / pageWidth;

    if (0.0f != fmodf(currentPage, 1.0f))
        pageControl.currentPage = currentPage + 1;
        pageControl.currentPage = currentPage;

    NSLog(@"Page Number : %ld", (long)pageControl.currentPage);

And if you are not using any pageControl, then ceil(currentPage) will be your current page number.