我有一个 UIPageViewController,我只是不知道如何知道用户翻页的方向,所以我可以适当地设置页数。
谢谢沙妮
正如赫加兹所说
在手势驱动转换完成后 ,调用此委托方法:
pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:
应该澄清的部分是completed
页面YES
是否完全翻过以及NO
页面实际上没有翻过。NO
例如,当用户只是拉起页面的一角,然后将其放回原处而不翻转页面时,就会发生这种情况。
这是您要实现的概念:
- (void)pageViewController:(UIPageViewController *)pvc didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
// If the page did not turn
if (!completed)
{
// You do nothing because whatever page you thought
// the book was on before the gesture started is still the correct page
return;
}
// This is where you would know the page number changed and handle it appropriately
// [self sendPageChangeNotification:YES];
}
在手势驱动转换完成后,调用此委托方法:
pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:
所以通过比较previousViewControllers
参数,pageViewController.viewControllers
你可以知道方向。
“基于页面的应用程序”模板提供以下两种方法:
- (NSUInteger)indexOfViewController:(DataViewController *)viewController;
给定视图控制器的查找索引的方法
- (DataViewController *)viewControllerAtIndex:(NSUInteger)index
一种在给定索引的情况下实例化视图控制器的方法。
为了制作正确的动画,您需要知道当前视图控制器的索引。基于页面的模板方法非常适合。然后,您只需比较“跳转到”索引和“当前”索引。
这里有一些代码来理解这个想法:
- (void)jumpToPage:(NSInteger)page {
// find current index
DataViewController *currentViewController = (DataViewController *)[self.pageViewController.viewControllers lastObject];
NSUInteger index = [self indexOfViewController:currentViewController];
// choosing the correct direction
// if the 'current' is smaller than the 'jump to' page, then choose forward
// vice versa
UIPageViewControllerNavigationDirection direction;
if (index < page) {
direction = UIPageViewControllerNavigationDirectionForward;
} else {
direction = UIPageViewControllerNavigationDirectionReverse;
}
// choose view controllers according to the orientation
NSArray *viewControllers;
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
DataViewController *rightViewController = [self viewControllerAtIndex:page];
viewControllers = [NSArray arrayWithObject:rightViewController];
} else {
DataViewController *rightViewController = [self viewControllerAtIndex:page];
DataViewController *leftViewController = [self viewControllerAtIndex:page-1];
viewControllers = [NSArray arrayWithObjects:leftViewController, rightViewController, nil];
}
// fire the method which actually trigger the animation
[self.pageViewController setViewControllers:viewControllers
direction:direction
animated:YES
completion:nil];
}
您可以将“pageIndex”属性添加到用作页面的视图控制器中。IOW,当您为 viewControllerBeforeViewController 和 viewControllerAfterViewController 创建视图控制器时(或当您调用 setViewControllers 时),将 pageIndex 存储在这些视图控制器中,然后您可以在需要知道索引时引用它。
great answers for page controller handling. I found that the view controller that was added as a page will call viewWillAppear as the user slides the page into view and will also call viewDidAppear upon completion.
setViewControllers:
当用户翻页时,将调用UIPageViewController 的方法。此方法接收一个类型的参数,该参数UIPageViewControllerNavigationDirection
将为您提供所需的信息。