How do you know what is the current page/view displayed inside an UIPageViewController
?
I have overridden the viewDidAppear
method of my child views, so that they send an id to the parent view in their viewDidAppear
method.
However, the problem is this: i cannot reliably use that id as id for the displayed page. because if the user turns the page but halfway through decides to stop the turning and put the page back, viewDidAppear
will already have been called. (the view is visible behind the curled page).
Maybe i should only switch to a new id if the current view disappears. But I wonder if there is not a more simple way to return the view that is currently visible?
It will return current page index. and must use this code under the delegate function of UIPageViewController (didFinishAnimating).
You should manually keep track of the current page. The delegate method
pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:
will tell you when to update that variable. The last argument of the methodtransitionCompleted:
can tell you whether a user completed a page turn transition or not.I first used Corey's solution but it wasn't working on iOS5 then ended up using,
It tried switching through different pages and it works well for now.
Below demo code (in Swift 2) that demonstrates how this is done by implementing a simple image swiper tutorial. Comments in the code itself :
I've been using
view.tag
for a while now, trying to keep track of the current page was too complicated.In this code the index is stored within the
tag
property of eachview
and is used to fetch the next or previous VC. Using this method it's also possible to create an infinite scroll. Check out the comment in code to view this solution as well:Thank for your answer guys, i faced similar problem, had to store index. I slightly modify my code, paste it below: