2013-12-18 85 views
8

我试图设置一个UIPageViewController,它可以通过NSUrlConnection数据拉动动态加载页面浏览量来无限滚动。我从3个视角开始:prev,curr,next;并在达到pageData阵列中的第一个或最后一个视图时加载附加视图。无限滚动UIPageViewController

问题是我在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]; 
     }); 
    } 
}]; 
*/ 


} 

UPDATE:

由于下面的快速响应,引用一个伟大的功能,我不知道的。这是预加载方法,完成后只需调用一次即可完成转换。这似乎很大程度上清除了不可靠的意见跳动和忘记位置。谢谢@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]; 
     } 
    } 
} 

回答

5

我想你需要在pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:处理程序中做你的加载调用。当动画完成并且新的ViewController变成活动的时候,这个方法将被调用。

+0

谢谢,这工作得很好!请参阅上面的更新以获取解这个功能非常干净。 – Miro

+0

Swift 3:func pageViewController(_ pageViewController:UIPageViewController,didFinishAnimating finished:Bool,previousViewControllers:[UIViewController],transitionCompleted completed:Bool) –