Search code examples
iosswiftanimationuipageviewcontroller

How to animate a container inside a page handled by UIPageViewController, and this animation being related to page swiping (page change)?


I have a working carrousel with a few pages and a login form on the last page, with a nice video playing behind. This carrousel is managed by a UIPageViewController, some content is fixed on background video view (which is my entry point, and from there, I instantiate the UIPageVIewController), and other content varies with page swapping. So in every page but the last I need to put a container with two buttons at the bottom of the view, and when the user swipes to the last page, I want this container to move down and disappear in the edge, with the same velocity of user swipe (linked to page swap). Can anybody help guide me to the best way of accomplishing that?


Solution

  • UIPageViewController inherits from UIViewController and even though it's most likely using UIScrollView to do its magic, the scroll view isn't exposed publicly so if you need a way to get accurate reporting on the scrolling position (which is required to accomplish what you want), you're not going to find it in UIPageViewController.

    If you want to do it right, you're going to have to use something other than UIPageViewController. You can easily accomplish the same functionality using a plain UIScrollView. Even a UICollectionView would do if you make its item size equal to your view's frame (or any other collection view trick, of which there are many).

    However, if you're also looking for the sexy page curl animations, that's also quite common these days via CATransaction and setting its type to pageCurl (see here).

    Once you implement your paging on a UIScrollView, or any of its subclasses, you can use its contentSize and contentOffset properties to tie in your custom animation (move out and disappear).