无限滚动UIPageViewController

我试图设置一个UIPageViewController ,可以通过NSUrlConnection数据拉dynamicdynamic加载页面浏览无限滚动。 我从3个观点开始:prev,curr,next; 并在pageData数组中的第一个或最后一个视图到达时加载另一个视图。

麻烦的是,我加载了viewControllerBeforeViewControllerviewControllerAfterViewController的额外的意见。 看起来,这些方法可以被称为2-3次之间做一个单一的页面之间滑动。 他们也可以在半滑动过程中调用,从不完成页面转换。 这可能意味着多个预加载开始过早完成。 然后以某种方式pageViewController忘记当前的位置。

 [self.pageViewController setViewControllers:@[[self.pageData objectAtIndex:gotoIdx]] direction:direction animated:NO completion:nil]; 

这行在下面用于表示在pageViewController添加数据,并转到相应的视图。 当我开始使用它时,而不是通过滑动进入prev / next视图,它开始跳转到看似随机的视图。 一旦我开始往前走时向后滑动…

是否有一个更适合的地方做预加载,只在页面转换完成后调用一次? 而且,是否必须调用上面的行,还是有一个更可靠的方法来确保它进入正确的页面? 感到沮丧尝试在这里连线。

  // the init line, somewhere early on. uses Scoll style self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:nil]; - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { NSUInteger index = [self.pageData indexOfObject:viewController]; if(index == 1){ // if loading last one on start, preload one more in advance [self loadSecondaryCalData:-1 endView:[[self viewControllerAtIndex:index-1] retain]]; } else if ((index == 0) || (index == NSNotFound)) { return nil; } index--; return [self viewControllerAtIndex:index]; } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { NSUInteger index = [self.pageData indexOfObject:viewController]; if(index == [self.pageData count]-2){ [self loadSecondaryCalData:1 endView:[[self viewControllerAtIndex:index+1] retain]]; // if last one, preload one more in advance } else if (index == NSNotFound) { return nil; } index++; if (index == [self.pageData count]) { return nil; } return [self viewControllerAtIndex:index]; } - (void)loadSecondaryCalData:(int)howMany endView:(CalendarViewController*)endView { // initiate NSURLConnection async request for view data } - (void)loadSecondaryCals: (NSDictionary*)data { // callback for async request // ... process view, cal // add new object to page data, at start or end if(next){ [self.pageData addObject:cal]; gotoIdx = [self.pageData count]-2; direction = UIPageViewControllerNavigationDirectionForward; } else { [self.pageData insertObject:cal atIndex:0]; gotoIdx = 1; direction = UIPageViewControllerNavigationDirectionReverse; } [self.pageViewController setViewControllers:@[[self.pageData objectAtIndex:gotoIdx]] direction:direction animated:NO completion:nil]; // alternate method to above line /* __block CalendarPageViewViewController *blocksafeSelf = self; __block int blocksafeGotoIdx = gotoIdx; __block UIPageViewControllerNavigationDirection blocksafeDirection = direction; [self.pageViewController setViewControllers:@[[self.pageData objectAtIndex:gotoIdx]] direction:direction animated:YES completion:^(BOOL finished){ if(finished) { dispatch_async(dispatch_get_main_queue(), ^{ [blocksafeSelf.pageViewController setViewControllers:@[[blocksafeSelf.pageData objectAtIndex:blocksafeGotoIdx]] direction:blocksafeDirection animated:NO completion:nil]; }); } }]; */ } 

更新:

由于下面的快速反应,引用了一个很好的function,我不知道。 这是预加载方法,完成时只需调用一次即可完成转换。 这似乎很大程度上清除了不可靠的跳跃和忘记位置。 谢谢@sha!

 // function to detect if we've reached the first or last item in views // detects for full completion only - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed { if(completed == TRUE && [previousViewControllers count] > 0 && [pageData count] > 1){ CalendarViewController *firstCal = [pageData objectAtIndex:0]; CalendarViewController *lastCal = [pageData objectAtIndex:([pageData count]-1)]; CalendarViewController *prevCal = [previousViewControllers objectAtIndex:0]; CalendarViewController *currCal = [[pageViewController viewControllers] objectAtIndex:0]; NSLog(@"transition completed: %@ -> %@", prevCal.week_day_date, currCal.week_day_date); if(currCal == firstCal){ // preload on begining [self loadSecondaryCalData:-1 endView:firstCal]; } else if(currCal == lastCal){ // preload to end [self loadSecondaryCalData:1 endView:lastCal]; } } } 

我想你需要在pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted: handler中进行加载调用。 当animation完成并且新的ViewController变成活动的时候,这个方法将被调用。