ios5 - 在横向时在页面视图控制器中翻页

Posted

技术标签:

【中文标题】ios5 - 在横向时在页面视图控制器中翻页【英文标题】:ios5 - turning pages in page view controller when in landscape 【发布时间】:2012-01-24 01:35:18 【问题描述】:

我在我的应用程序中使用 UIPageViewController 和故事板。 在纵向时,我使用以下代码从一个页面跳转到另一个页面并且它工作正常。

int direction = UIPageViewControllerNavigationDirectionForward;
if ([self.modelController currentPage] < pagenum)

    direction = UIPageViewControllerNavigationDirectionForward;

else if ([self.modelController currentPage] > pagenum)

    direction = UIPageViewControllerNavigationDirectionReverse;


[self.pageViewController setViewControllers:[NSArray arrayWithObject:[self.modelController viewControllerAtIndex:pagenum storyboard:self.storyboard]] direction:direction animated:YES completion:NULL];    

但是当我们处于横向模式时,相同的代码不起作用。横屏时如何翻页?

【问题讨论】:

我在我的应用程序中尝试了很多方法来实现这种卷曲效果,但我无法理解 UIPageViewController。你能和我分享一个简单的演示示例吗? @CocoaMatters,我没有演示应用程序......但只需创建一个页面视图控制器项目,所有功能都已为您准备好。它也会生成演示页面 你能给我发邮件到 hiren.bhadreshwara@gmail.com 吗? 【参考方案1】:

如果您查看 Xcode 中的 Page-Based Application Template,您会发现以下 UIPageViewControllerDelegate 方法:

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation

    if (UIInterfaceOrientationIsPortrait(orientation)) 
        // In portrait orientation: Set the spine position to "min" and the page view controller's view controllers array to contain just one view controller. Setting the spine position to 'UIPageViewControllerSpineLocationMid' in landscape orientation sets the doubleSided property to YES, so set it to NO here.
        UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
        NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];

        self.pageViewController.doubleSided = NO;
        return UIPageViewControllerSpineLocationMin;
    

    // In landscape orientation: Set set the spine location to "mid" and the page view controller's view controllers array to contain two view controllers. If the current page is even, set it to contain the current and next view controllers; if it is odd, set the array to contain the previous and current view controllers.
    DataViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
    NSArray *viewControllers = nil;

    NSUInteger indexOfCurrentViewController = [self.modelController indexOfViewController:currentViewController];
    if (indexOfCurrentViewController == 0 || indexOfCurrentViewController % 2 == 0) 
        UIViewController *nextViewController = [self.modelController pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
        viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil];
     else 
        UIViewController *previousViewController = [self.modelController pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
        viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil];
    
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];


    return UIPageViewControllerSpineLocationMid;

当从上述方法返回 UIPageViewControllerSpineLocationMid 时,您告诉 UIPageViewController 它应该期望并排显示 两个 UIViewControllers(即两个页面)。

这里的关键是在调用UIPageViewController的setViewControllers:direction:animated:completion:方法时必须传递正确数量的UIViewControllers。

显示两页?传递两个 UIViewController。 显示一页?传递一个 UIViewController。

您提供的代码永远不会将多个 UIViewController 传递给 UIPageViewController。

如果 UIPageViewControllerSpineLocation 与您传递的 UIViewController 的数量不对应,它将崩溃或什么也不做。

如果您需要进一步的帮助,请告诉我。

【讨论】:

我们可以添加两个以上的视图控制器,甚至在 pageviewcontroller 中添加两个。请指导我。

以上是关于ios5 - 在横向时在页面视图控制器中翻页的主要内容,如果未能解决你的问题,请参考以下文章

《iOS Human Interface Guidelines》——Page View Controller

我们如何像在 iphone 应用程序中翻页一样翻转视图?

在一个视图控制器中强制横向

在 UIPageViewController 中实例化页面后非常不稳定、缓慢的翻页

如何仅在应用程序第一次运行时在我的 iOS 应用程序中打开页面视图控制器?

在横向模式下制作 iOS 应用