5

我看到UIPageViewControllerusingScroll转换与PageCurl. PageCurl 以我期望的方式工作,但是在使用 Scroll-ing 时,我有时会看到额外的(无意义的)调用viewControllerBeforeViewController:viewControllerAfterViewController: 是否有人认识到这种额外数据源调用的模式?

我正在显示一系列图像,我可以从序列中的任何位置开始。当使用 Scroll 转换而不是 PageCurl 时,如果我移动到“右侧”,则在显示初始页面后,我会收到 3 个 DataSource 调用,而不是我期望的单个调用。对于下面运行的示例,我从索引值 3 开始。当我滑动移动到“下一个”图像时,我希望viewControllerAfterViewController:对索引 3 进行一次调用以到达索引 4。如果我运行测试,我会得到两个额外的调用:一个用于索引 3 之前(即 2),一个用于索引 4 之后(即 5)。输出是:

2013-04-19 12:37:33.964 Clouds[496:907] Page 3 - viewControllerAfterViewController called on this index
2013-04-19 12:37:33.988 Clouds[496:907] Page 3 - viewControllerBeforeViewController called on this index
2013-04-19 12:37:34.010 Clouds[496:907] willTransitionToViewControllers to indices
2013-04-19 12:37:34.014 Clouds[496:907] Page 4
2013-04-19 12:37:34.461 Clouds[496:907] Page 4 - viewControllerAfterViewController called on this index

在这一切之后显示的视图确实是索引 4 的视图。在第一次转换之后, UIPageViewController 的行为似乎符合我的预期。当我使用 PageCurl 转换时,它总是按照我期望的方式运行,并且我没有收到任何这些无关的调用。

实际产生输出的代码是:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;


    CloudImageVC *startingViewController = [self viewControllerAtIndex:self.currentPage storyboard:self.storyboard];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];

    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];

    CGRect pageViewRect = self.view.bounds;
    self.pageViewController.view.frame = pageViewRect;

    [self.pageViewController didMoveToParentViewController:self];

    // Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
    self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}

- (CloudImageVC *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard {
    // Return the data view controller for the given index.
    if (index >= [self.imageNames count]) {
        return nil;
    }
    self.dataViewController = [self makeDataViewControllerAtIndex:index storyboard:storyboard];
    return self.dataViewController;
 }

- (CloudImageVC *) makeDataViewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard {
    // Make and return the data view controller for the given index.
    if (index >= [self.imageNames count]) {
        return nil;
    }
    // Create a new view controller and pass suitable data.
    self.dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"CloudImageVC"];
    self.dataViewController.imageIndex = index;
    //self.dataViewController.cloudImage = nil;
    return self.dataViewController;
}

- (NSUInteger)indexOfViewController:(CloudImageVC *)viewController
{
    // Return the index of the given data view controller.
    // For simplicity, we store the index value in the view controller.
    return viewController.imageIndex;
}

#pragma mark - Page View Controller Data Source

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [self indexOfViewController:(CloudImageVC *)viewController];
    NSLog(@"Page %d - viewControllerBeforeViewController called on this index", index);
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }
    index--;
    return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [self indexOfViewController:(CloudImageVC *)viewController];
    NSLog(@"Page %d - viewControllerAfterViewController called on this index", index);
    if (index == NSNotFound) {
        return nil;
    }

    index++;
    if (index == self.imageNames.count) {
        return nil;
    }
    return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}

#pragma mark - Protocol UI PageViewController Delegate

- (void) pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {
    NSLog(@"willTransitionToViewControllers to indices");
    for (CloudImageVC *vc in pendingViewControllers) {
        NSLog(@"Page %d",vc.imageIndex);
    }
}
4

1 回答 1

0

答案在 rdelmar 的评论中。

于 2013-11-06T01:21:39.397 回答