Lolloz89
Lolloz89

Reputation: 2809

UIPageViewController delay page change animation

I am developing an application that makes use of a UIPageViewController. I noticed that if I change multiple pages too quickly, it causes several problems at runtime.

Is there a way to set a delay (such 2 or 3 milliseconds) between two page changes? Thanks in advance.

************ DETAILED ANSWER **************

The solution is this:

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{

    if(completed) {
        [pageViewController.view setUserInteractionEnabled:NO];
        [self performSelector:@selector(enableUserInteraction) withObject:nil afterDelay:0.2];
    }
}

-(void)enableUserInteraction{
    [self.view setUserInteractionEnabled:YES];
}

Upvotes: 4

Views: 2335

Answers (2)

Tibidabo
Tibidabo

Reputation: 21571

I put it in the pageViewController:willTransitionToViewControllers: and used dispatch_after. With this solution the user cannot swipe quickly 2-3 times like in pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
pageViewController.view.userInteractionEnabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    pageViewController.view.userInteractionEnabled = YES;

});

}

Upvotes: 0

Gabriel Lim
Gabriel Lim

Reputation: 156

In your animation block, set userInteraction = NO until the animation finishes. This mean that the user will not be able to interact with the screen and thus change the page until it finishes animating.

Upvotes: 3

Related Questions