Created
April 2, 2014 20:45
-
-
Save itsthejb/9942782 to your computer and use it in GitHub Desktop.
UIPageViewControllerEndlessDataSource: provides an 'endlessly' scrolling wrapper for `UIPageViewControllerDataSource`. Is also rather simpler to set up.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIPageViewControllerEndlessDataSource.h | |
// Created by Jonathan Crooke on 02/04/2014. | |
// | |
#import <UIKit/UIKit.h> | |
/** | |
* This data source protocol provides a wrapper for `UIPageViewControllerDataSource` | |
*/ | |
@protocol UIPageViewControllerEndlessDataSource <NSObject> | |
@required | |
/** | |
* Your data source must provide the total number of items to be displayed. | |
* | |
* @param pageViewController The page view controller | |
* | |
* @return Total count of pages | |
*/ | |
- (NSUInteger) numberOfPagesForPageViewController:(UIPageViewController*) pageViewController; | |
/** | |
* Provide a page at the given index. | |
* | |
* @param pageViewController The page view controller | |
* @param index Index to be displayed | |
* | |
* @return View controller for the given index | |
*/ | |
- (UIViewController*) pageViewController:(UIPageViewController*) pageViewController | |
viewControllerAtIndex:(NSUInteger) index; | |
/** | |
* Your data source must be able to derive an index from a view controller. | |
* | |
* @param pageViewController The page view controller | |
* @param controller An already displayed view controller | |
* | |
* @return Index for the specified view controller | |
*/ | |
- (NSUInteger) pageViewController:(UIPageViewController*) pageViewController | |
indexOfViewController:(UIViewController*) controller; | |
@end | |
/** | |
* Data source wrapper | |
*/ | |
@interface UIPageViewControllerEndlessDataSource : NSObject <UIPageViewControllerDataSource> | |
/** | |
* Designated convenience initializer. | |
* | |
* @param dataSource Data source object conforming to `UIPageViewControllerEndlessDataSource` | |
* | |
* @return Instantiated data source to apply to `-[UIPageViewController datasSource]` | |
*/ | |
+ (instancetype) endlessDataSourceWithDataSource:(id <UIPageViewControllerEndlessDataSource>) dataSource; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIPageViewControllerEndlessDataSource.m | |
// Created by Jonathan Crooke on 02/04/2014. | |
// | |
#import "UIPageViewControllerEndlessDataSource.h" | |
@interface UIPageViewControllerEndlessDataSource () | |
@property (nonatomic, weak) id <UIPageViewControllerEndlessDataSource> datasource; | |
@end | |
@implementation UIPageViewControllerEndlessDataSource | |
+ (instancetype) endlessDataSourceWithDataSource:(id <UIPageViewControllerEndlessDataSource>) dataSource { | |
UIPageViewControllerEndlessDataSource *source = [[UIPageViewControllerEndlessDataSource alloc] init]; | |
source.datasource = dataSource; | |
return source; | |
} | |
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController | |
viewControllerBeforeViewController:(UIViewController *)viewController | |
{ | |
NSUInteger idx = [self.datasource pageViewController:pageViewController | |
indexOfViewController:viewController]; | |
if (idx > 0) { | |
// page back | |
return [self.datasource pageViewController:pageViewController | |
viewControllerAtIndex:idx - 1]; | |
} else { | |
// cycle to end | |
NSUInteger num = [self.datasource numberOfPagesForPageViewController:pageViewController]; | |
return [self.datasource pageViewController:pageViewController viewControllerAtIndex:num - 1]; | |
} | |
} | |
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController | |
viewControllerAfterViewController:(UIViewController *)viewController | |
{ | |
NSUInteger idx = [self.datasource pageViewController:pageViewController | |
indexOfViewController:viewController]; | |
NSUInteger num = [self.datasource numberOfPagesForPageViewController:pageViewController]; | |
if (idx < num - 1) { | |
// page forward | |
return [self.datasource pageViewController:pageViewController | |
viewControllerAtIndex:idx + 1]; | |
} else { | |
// cycle back to beginning | |
return [self.datasource pageViewController:pageViewController | |
viewControllerAtIndex:0]; | |
} | |
} | |
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController { | |
if ([self.datasource respondsToSelector:_cmd]) { | |
return [(id) self.datasource presentationCountForPageViewController:pageViewController]; | |
} | |
return 0; | |
} | |
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController { | |
if ([self.datasource respondsToSelector:_cmd]) { | |
return [(id) self.datasource presentationIndexForPageViewController:pageViewController]; | |
} | |
return 0; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment