我看到使用UIPageViewController
转换的Scroll
与PageCurl
之间的行为不同。 PageCurl以我期望的方式工作,但在使用滚动时,我有时会看到对viewControllerBeforeViewController:
和viewControllerAfterViewController:
的额外(荒谬)调用
有没有人认识到这种额外的数据源调用模式?
我正在显示一系列图像,我可以从序列的任何地方开始。
当使用Scroll转换但不是PageCurl时,如果我移动到“右”,则显示初始页面后,我得到3个DataSource调用而不是我期望的单个调用。对于下面的示例运行,我从我的索引值3开始。当我滑动移动到“下一个”图像时,我希望在索引3上获得单个viewControllerAfterViewController:
调用以获得索引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);
}
}
答案 0 :(得分:0)
答案在rdelmar的评论中。