ios 我怎么知道 UIPageViewController 是向前还是向后翻转?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8751633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:09:43  来源:igfitidea点击:

How can i know if UIPageViewController flipped forward or reversed?

iosuipageviewcontroller

提问by shannoga

I have a UIPageViewController and I just can not figure out how to know to what direction the user turned the page so i can set the page count appropriately.

我有一个 UIPageViewController,但我不知道如何知道用户翻页的方向,所以我可以适当地设置页数。

Thanks Shani

谢谢沙妮

采纳答案by jonkroll

When the user turns a page the UIPageViewController's setViewControllers:method will be called. This method receives an argument of type UIPageViewControllerNavigationDirectionthat will give you the information you need.

当用户翻页时,setViewControllers:将调用UIPageViewController 的方法。此方法接收一个类型的参数,该参数UIPageViewControllerNavigationDirection将为您提供所需的信息。

回答by roff

As Hejazi said

正如 Hejazi 所说

After a gesture-driven transition completes this delegate methodis called:

pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:

在手势驱动的转换完成后,将 调用此委托方法

pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:

The part that should be clarified is that completedwill be YESif the page was fully turned and will be NOif the page did not actually turn. The NOcase happens, for example, when the user just pulls up the corner of the page and then puts it back down without flipping the page.

应该澄清的是,completedYES该页是否完全导通,并将于NO如果页面实际上并没有打开。NO例如,当用户只是拉起页面的一角然后将其放回原处而没有翻转页面时,就会发生这种情况。

This is the concept you will want to implement:

这是您将要实施的概念:

- (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];
}

回答by Hejazi

After a gesture-driven transition completes this delegate methodis called:

在手势驱动的转换完成后,将调用此委托方法

pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:

So by comparing the previousViewControllersparameter and pageViewController.viewControllersyou can know the direction.

所以通过比较previousViewControllers参数pageViewController.viewControllers就可以知道方向了。

回答by Ikhsan Assaat

The 'Page-Based Application' template provide these 2 methods :

“基于页面的应用程序”模板提供了以下两种方法:

- (NSUInteger)indexOfViewController:(DataViewController *)viewController; 

a method for finding index given a view controller

一种在给定视图控制器的情况下查找索引的方法

- (DataViewController *)viewControllerAtIndex:(NSUInteger)index

a method for instantiating a view controller given an index.

一种在给定索引的情况下实例化视图控制器的方法。

For making the correct animation, you need to know the index of your current view controller. The page-based template methods are perfect fit for that. Then, you simply compare your 'jump to' index and your 'current' index.

为了制作正确的动画,您需要知道当前视图控制器的索引。基于页面的模板方法非常适合这种情况。然后,您只需比较“跳转到”索引和“当前”索引。

Here's some code to get the idea :

这里有一些代码来了解这个想法:

- (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];
}

回答by Mark Krenek

You could add a "pageIndex" property to your view controllers that serve as the pages. IOW, when you create the view controllers for viewControllerBeforeViewController & viewControllerAfterViewController (or when you call setViewControllers), store a pageIndex in those view controllers that you can then reference whenever you need to know the index.

您可以向用作页面的视图控制器添加“pageIndex”属性。IOW,当您为 viewControllerBeforeViewController 和 viewControllerAfterViewController 创建视图控制器时(或当您调用 setViewControllers 时),在这些视图控制器中存储一个 pageIndex,然后您可以在需要知道索引时引用它。

回答by cynistersix

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.

页面控制器处理的好答案。我发现作为页面添加的视图控制器会在用户将页面滑入视图时调用 viewWillAppear,并且在完成时也会调用 viewDidAppear。