@Daniel your link is broken and your "answer" does little to explain your solution to the question. For those interested in reading documentation for the UIPageViewController Class.
The method to pay attention to here is :
- (void)setViewControllers:(NSArray *)viewControllers
direction:(UIPageViewControllerNavigationDirection)direction
animated:(BOOL)animated
completion:(void (^)(BOOL finished))completion
The UIPageViewController expects you to tell it what data to deliver and what controller to use based on a value. Here I am telling my UIPageController Subclass to be the delegate and dataSouce. After configuring the delegate methods and the viewControllerAtIndex dataSource method, I added a custom interval which calls my loadNextController method. In that method I am checking to see if the "next" controller is nil. If it is not nil I pass the "next" controller as the @[starting controller] back to a second call to setViewControllers. If it is nil I just reset the index value to ZERO and call setViewControllers with my "next" controller as the @[starting controller]. This custom subclass serves to page to the next or first controller automatically. The bonus is that user interaction is still handled by the UIPageViewController Class.
Note: The initial call to setViewControllers is required because your UIPageViewContrller will not know what if any viewController can be loaded first. My second call to setViewControllers does nothing more than skip to the next or first viewController.
- (void)viewDidLoad {
[super viewDidLoad];
self.index = 0;
self.delegate = self;
self.dataSource = self;
/*! setup an interval */
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(loadNextController)
userInfo:nil
repeats:YES];
/*! set starting controller */
NSArray *startingViewControllers = @[[self viewControllerAtIndex:self.index]];
[self setViewControllers: startingViewControllers
direction: UIPageViewControllerNavigationDirectionForward
animated: YES
completion: nil];
}
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = ((YSPageContentViewController*) viewController).pageIndex;
if (index > 0 ) {
return [self viewControllerAtIndex:index-1];
}
return nil;
}
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = ((YSPageContentViewController*) viewController).pageIndex;
if (index+1 < [self.items count] ) {
return [self viewControllerAtIndex:index+1];
}
return nil;
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index{
if (index < [self.items count]) {
PageContentViewController *pageContentViewController = [[PageContentViewController alloc]init];
pageContentViewController.pageIndex = index;
pageContentViewController.image = (UIImage*)self.items[index];
return pageContentViewController;
}
return nil;
}
/*! set next controller */
- (void)loadNextController {
PageContentViewController *nextViewController = [self viewControllerAtIndex:self.index++];
if (nextViewController == nil) {
self.index = 0;
nextViewController = [self viewControllerAtIndex:self.index];
}
[self setViewControllers:@[nextViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
}
Note: This does work; however I am not always returning the correct controller when the internal gesture recognizer changes the controller. Somewhere along the way I am losing track of the index. I will update this with my finished implementation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…