无限滚动UIPageViewController

时间:2013-12-18 02:30:13

标签: ios objective-c uipageviewcontroller

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

问题是我在viewControllerBeforeViewControllerviewControllerAfterViewController中加载了其他观看次数。在页面之间进行单次滑动时,似乎可以调用这些方法2-3次。它们也可以在从不完成页面转换的半滑动期间调用。这可能意味着多个预加载开始过早完成。然后以某种方式pageViewController忘记当前位置。

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

下面使用此行来表示pageViewController中的数据添加,并转到适当的视图。当我开始使用它时,它不是通过滑动进入上一个/下一个视图,而是开始跳转到看似随机的视图。一旦我开始向后滑动......

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

 // 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];
        });
    }
}];
*/


}

更新:

感谢下面的快速响应,引用了一个我不知道的强大功能。这是预加载方法,只有在完成转换时才能完美地调用一次。这似乎已经大大消除了不可靠的跳跃视图和忘记位置。谢谢@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];
        }
    }
}

1 个答案:

答案 0 :(得分:5)

我认为你需要在pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:处理程序中进行加载调用。当动画完成并且新的ViewController成为活动的时,将调用此方法。