Getting the current page

69,231

Solution 1

There is no UIScrollView property for the current page. You can calculate it with:

int page = scrollView.contentOffset.x / scrollView.frame.size.width;

If you want to round up or down to the nearest page, use:

CGFloat width = scrollView.frame.size.width;
NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;

Solution 2

I pretty recommend you to use this code

int indexOfPage = scrollView.contentOffset.x / scrollView.frame.size.width;

but if you use this code your view doesn't need to be exactly on the page that indexOfPage gives you. It because I also recommend you to use this code only in this method

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{}

which is called when your scrollView finishes scrolling and to have number of your page really sharp

I recommend you to set your scrollView to paged enabled with this code

[scrollView setPagingEnabled:YES];

So finally it should look like that way

-(void) methodWhereYouSetYourScrollView
{
   //set scrollView
   [scrollView setPagingEnabled:YES];
   scrollView.delegate = self;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   int indexOfPage = scrollView.contentOffset.x / scrollView.frame.size.width;
   //your stuff with index
}

Solution 3

In swift I would do it in extension:

extension UIScrollView {
    var currentPage:Int{
        return Int((self.contentOffset.x+(0.5*self.frame.size.width))/self.frame.width)+1
    }
}

Then just call:

scrollView.currentPage

Solution 4

As above but as a category


@interface UIScrollView (CurrentPage)
-(int) currentPage;
@end
@implementation UIScrollView (CurrentPage)
-(int) currentPage{
    CGFloat pageWidth = self.frame.size.width;
    return floor((self.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}
@end

Solution 5

Another way:

extension MyViewController: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let width = scrollView.frame.width
        let page = Int(round(scrollView.contentOffset.x/width))
        print("CurrentPage:\(page)")
    }
}
Share:
69,231

Related videos on Youtube

TheNeil
Author by

TheNeil

Interested in iOS development, music, photography, video games, books, and other typical things.

Updated on May 23, 2020

Comments

  • TheNeil
    TheNeil almost 4 years

    In my scroll view, I want to get the current page that's being displayed (maybe page isn't the correct term). I can't find any variable that holds this. But I think it must be held somewhere, since the indicator is able to show which sub-view of the scroll view is currently being displayed.

    Is this hidden from us completely or is there a way for me to access it?

  • Leonard Pauli
    Leonard Pauli about 11 years
    That's overdone, just use return round(self.contentOffset.x / self.frame.size.width); Much simpler and easier to understand..
  • Admin
    Admin almost 11 years
    This is greedy, not lazy... It also isn't MVC. A much simpler way is to invalidate the currentPage on scroll, and set it only when it's requested... all from a subclass or category on UIScrollView
  • Sam Spencer
    Sam Spencer almost 11 years
    Don't forget to use the UIScrollViewDelegate to get the current page when the user scrolls!
  • Klaas
    Klaas over 10 years
    Casting to int and then assigning to a float? Why not make pageNum int as well ;)
  • Jason McCreary
    Jason McCreary over 10 years
    This may have problems when dealing with rotation.
  • user
    user about 10 years
    -1 @LeonardPauli. The above implementation returns the page that is more than 50% visible. The naive implementation only changes page value when the scrollview's subview occupies the entire scrollview bounds.
  • Leonard Pauli
    Leonard Pauli about 10 years
    @user Firstly, floor() rounds always down, and round() rounds up if decimal part is >= 5, else down. By that being said, floor(a)=round(a-0.5). Let's do some simple algebra. floor((a-b/2)/b)+1 = floor(a/b-0.5)+1 = round(a/b-0.5-0.5)+1 = round(a/b-1)+1 = round(a/b). Look! It seems like floor((a-b/2)/b)+1 = round(a/b)! Mathemagical.
  • Leonard Pauli
    Leonard Pauli about 10 years
    But yeah, if I for some reason would have used floor instead, then you would have been perfectly right! ;)
  • Vassily
    Vassily about 9 years
    not working on iOS7 device (compiled with XCODE 6.2) contentOffset.x equals 639 instead of 640 (don't ask me why...) when scrollViewDidEndDecelerating. user363349 answer is correct : return floor((self.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
  • fnc12
    fnc12 almost 9 years
    please add swift version
  • Rodrigo Ruiz
    Rodrigo Ruiz about 8 years
    This not always works, sometimes the frame width is wrong.
  • mvandillen
    mvandillen over 7 years
    Almost perfect, I had to remove the +1 at the end since everything is zero based in my world
  • Sneha
    Sneha over 6 years
    This does not work always. Sometimes I'm getting the page as 0 which is wrong in my case.
  • Sneha
    Sneha over 6 years
    This does not work always. Sometimes I'm getting the page as 0 which is wrong in my case.