I have a UIViewController in my app with a navigation bar as shown in the below gif. The UIViewController contains a UIPageViewController with 4 pages. Each tab on the navigation bar represents one page from the UIPageViewController.
I am trying to implement a smooth scrolling between these UIPageViewController pages i.e if I am currently on the first tab and I click on the last tab on the navigation bar, the UIPageViewController must smoothly scroll through all the tabs to the last one. Though this works right now, I am not happy with the scroll performance as it looks jittery. I am using the
- setViewControllers:direction:animated:completion:
api to scroll between the pages.
This is what my code looks like
- (void)navigateToNextPage {
//you have navigated to the destination, return
if(self.destinationPageIndex == self.currentPageIndex)
return;
if(self.destinationPageIndex < 0 || self.destinationPageIndex >= [self.pageViewControllerIdentifiers count])
return;
//determine the scroll direction
UIPageViewControllerNavigationDirection direction = self.destinationPageIndex < self.currentPageIndex ? UIPageViewControllerNavigationDirectionReverse : UIPageViewControllerNavigationDirectionForward;
//get the index of the next page to scroll to
NSInteger nextPageIndex = self.destinationPageIndex < self.currentPageIndex ? self.currentPageIndex-1 : self.currentPageIndex+1;
//get the storyboard identifier of the page to navigate to
NSString *identifier = [self.pageViewControllerIdentifiers objectAtIndex:nextPageIndex];
NSString *storyboardName = @"Sales";
id viewController = [COSCheckoutNavigationViewController instantiateControllerFromStoryboardName:storyboardName storyboardIdentifier:identifier];
NSArray *viewControllers = @[viewController];
__weak typeof (self) weakSelf = self;
[self.pageViewController setViewControllers:viewControllers direction:direction animated:YES completion:^(BOOL finished) {
[weakSelf updateCurrentPageIndex];
//navigate to the next page after small delay
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf navigateToNextPage];
});
}];
}
Is there a way I could improve upon this code for better scroll performance? Would any other technique help me achieve what I'm trying?