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?
I extracted this out of a couple of our apps...
Full source here
For swift I would just use this
Pretty straightforward, it basically takes the scrollview x position, divides it by the scrollview width, then rounds it.
There is no
UIScrollView
property for the current page. You can calculate it with:If you want to round up or down to the nearest page, use:
I pretty recommend you to use this code
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
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
So finally it should look like that way
The solution of Aoakenfo is amazing, this is another solution combining your code with the function scrollViewDidScroll and PageController
As above but as a category