Getting the current page
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;
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}
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