我正在尝试将UIPageViewController
的过渡方式从UIPageViewControllerTransitionStylePageCurl
切换为UIPageViewControllerTransitionStyleScroll
。
使用UIPageViewControllerTransitionStylePageCurl
时,页面会根据需要进行转换。但是,对于UIPageViewControllerTransitionStyleScroll
,页面转弯之间会出现空白页面。空白页面显示的模式在构建之间不一致。
知道这里可能会发生什么吗?我很难过。
//AppDelegate.m
PageVC *pageVC = [[PageVC alloc]
initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:NULL];
//PageVC.h
@interface PageVC : UIPageViewController <UIPageViewControllerDataSource>
//PageVC.m
- (void)viewDidLoad {
[super viewDidLoad];
self.graphVC = [[GraphVC alloc]init];
self.aboutVC = [[AboutVC alloc] init];
self.exerciseVC = [[ExerciseVC alloc] init];
self.viewControllerArray = [[NSArray alloc] initWithObjects:self.exerciseVC, self.graphVC, self.aboutVC, nil];
self.currentPage = 1;
if (self.graphVC != nil) {
self.dataSource = self;
[self setViewControllers:@[self.graphVC]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];
}}
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (self.currentPage >= 2) {
return nil;
} else {
self.currentPage += 1;
return [self.viewControllerArray objectAtIndex:self.currentPage];
}
}
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (self.currentPage <= 0) {
return nil;
} else {
self.currentPage -= 1;
return [self.viewControllerArray objectAtIndex:self.currentPage];
}
}
答案 0 :(得分:1)
作为@pbasdf worote,在currentPage
/ viewControllerAfter
中跟踪Before
是一个坏主意。使用TransitionStyleScroll
时,每页翻页会多次调用这两种方法。结果,它们有时会错误地增加并返回零。 (TransitionStylePageCurl
仅在翻页后调用每个方法一次。)
更好的解决方案是检查传递给每个方法的viewController
,然后相应地返回:
// PageVC.m
- (void)viewDidLoad {
[super viewDidLoad];
// initialize array of ViewControllers
viewControllerArray = [[NSArray alloc] initWithObjects:exerciseVC, graphVC, aboutVC, nil];
}
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [viewControllerArray indexOfObject:viewController];
if (index >= [viewControllerArray count]-1) {
return nil;
} else {
return [viewControllerArray objectAtIndex:index+1];
}
}
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [viewControllerArray indexOfObject:viewController];
if (index <= 0) {
return nil;
} else {
return [viewControllerArray objectAtIndex:index-1];
}