Skip to content

Instantly share code, notes, and snippets.

@vmish
Last active April 26, 2016 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmish/0d6d1a897dcd4b7326b0e454da1844ce to your computer and use it in GitHub Desktop.
Save vmish/0d6d1a897dcd4b7326b0e454da1844ce to your computer and use it in GitHub Desktop.
UIPageViewController
#import <UIKit/UIKit.h>
@interface MainContentViewController : UIViewController
@property (nonatomic) NSInteger index;
@end
#import "MainViewController.h"
@interface MainViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
@end
@implementation MainViewController
{
UIPageViewController *pageViewController;
UIPageControl *pageControl;
NSUInteger pageCount;
}
- (void)viewDidLoad
{
[super viewDidLoad];
pageCount = 8;
pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
[pageViewController setViewControllers:@[[self viewControllerAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
pageViewController.dataSource = self;
pageViewController.delegate = self;
[self addChildViewController:pageViewController];
[self.view addSubview:pageViewController.view];
[pageViewController didMoveToParentViewController:self];
}
- (UIViewController *)viewControllerAtIndex:(NSInteger)index
{
if (index < 0 || index > pageCount-1) return nil;
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainContentViewController"];
[vc setValue:@(index) forKey:@"index"];
vc.view.tag = index;
else vc;
}
- (void)pageViewController:(UIPageViewController *)pageViewCntr didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
NSInteger currentIndex = ((UIViewController *)pageViewController.viewControllers.firstObject).view.tag;
if (completed) pageControl.currentPage = currentIndex;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSInteger index = ((MainContentViewController *)viewController).index;
if (index == NSNotFound || !index) return nil;
return [self viewControllerAtIndex:index-1];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSInteger index = ((MainContentViewController *)viewController).index;
if (index == NSNotFound || index == pageCount-1) return nil;
return [self viewControllerAtIndex:index+1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment