Search code examples
iosuipageviewcontroller

control UIPageViewController movement programmatically


I have two buttons which can be used to start the page transition in a UIPageViewController. I start the transition from one page to another programmatically like this:

//to go left
[_pageVC setViewControllers:@[[self pageViewController:_pageVC viewControllerBeforeViewController:[_pageVC.viewControllers lastObject]]]
                      direction:UIPageViewControllerNavigationDirectionReverse
                       animated:YES
                     completion:^(BOOL finished) { }];

The problems is that the position of the buttons make it very easy to tap them several times fast, which causes unwanted behaviour and even crashes the app. So I would like to deactivate them while the pages are transitioning.

For that purpose I created a BOOL, which I set to YES when the animation starts, but I do not know where to set it to NO again. The completition block in the function above is called too early and pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted: does not get called if the transition was started programatically (from the docs: Called after a gesture-driven transition completes).

How can I deactivate the buttons while the transition is being executed?


Solution

  • I have used the solution from pbasdf to disable the buttons, but it probably was already working correctly. I realised that the problem wasn't so much the disabling of the buttons, but that the swiping was still active, causing strange behaviour.

    I disable and enable the swiping when the buttons are enabled/disabled with this method:

    -(void)setPVScrollEnabled:(BOOL)scrollEnabled
    {
        for (UIScrollView *view in self.pageViewController.view.subviews) {
    
            if ([view isKindOfClass:[UIScrollView class]]) {
    
                view.scrollEnabled = scrollEnabled;
            }
        }
    }