ios - 无限滚动 UIPageViewController

标签 ios objective-c uipageviewcontroller

我正在尝试设置一个 UIPageViewController,它可以通过 NSUrlConnection 数据拉取动态加载页面 View 来无限滚动。我从 3 个 View 开始:prev、curr、next;并在到达 pageData 数组中的第一个或最后一个 View 时加载另一个 View 。

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

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

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

有没有更合适的地方做页面转换完成后只调用一次的预加载?而且,是否必须调用上面的行,或者是否有更可靠的方法来确保它转到正确的页面?尝试将其连接到此处感到沮丧。

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


}

更新:

感谢下面的快速响应,引用了一个我不知道的很棒的函数。这是 preload 方法,非常完美,只在转换完成时调用一次。这似乎大大消除了不可靠的 View 跳转和位置遗忘。谢谢@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: 处理程序中进行加载调用。当动画结束并且新的 ViewController 变为事件状态时,将调用此方法。

关于ios - 无限滚动 UIPageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20648679/

相关文章:

iphone - 取回 uiwebview 的 URL?

ios - 在 UIPageView 最后一页上滑动 Segue

ios - 使用 StoryBoard 的 PDF 查看器

ios - 使用dylib创建ios框架

ios - Iphone 开机黑屏

ios - Xamarin.iOS 错误 : This copy of libswiftCore. dylib 需要 10.14.4 之前的操作系统版本

ios - UITableViewCell 向右滑动触发 Action 或执行 segue

ios - 在 iPhone 上处理时如何使 iPad 上的 iOS 推送通知无效?

ios - UIButton 不显示在自定义 UIView 中

ios - 对不同的页面 View 使用相同的 ViewController Swift