UIPageviewController - 在不改变方向的情况下从两种页面模式切换到一种页面模式

时间:2012-12-08 11:08:32

标签: iphone ipad uipageviewcontroller

我正在使用UIPageViewController一次显示两个视图控制器。在显示几个这样的视图后,我想显示单视图控制器而不改变方向,它固定为横向。我在iPad上制作。

以下是我正在使用的代码:

-(id)initWithViewControllers:(NSMutableArray *)Controllers
{
self = [super initWithNibName:nil bundle:nil];
if(self)
{
    self.VControllers =  Controllers;

    pageController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.view.frame = CGRectMake(12.0f, 10.0f, 748.0f, 985.0f);
    self.pageController.delegate = self;
    self.pageController.dataSource = self;
    [self.pageController setViewControllers:[NSArray arrayWithObject:[VControllers objectAtIndex:0] ] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished)
     {
     }];
    [self addChildViewController:self.pageController];
    [self.view addSubview:self.pageController.view];   
}
return self;
}

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
               spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if(UIInterfaceOrientationIsPortrait(orientation))
{
 //Set the array with only 1 view controller
    UIViewController *currentViewController = [self.pageController.viewControllers objectAtIndex:0];
    NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];

    //Important- Set the doubleSided property to NO.
    self.pageController.doubleSided = NO;
    //Return the spine location
    return UIPageViewControllerSpineLocationMin;

}

else
{
NSArray *viewControllers = nil;

    exampleViewController *currentViewController = [self.pageController.viewControllers objectAtIndex:0];

    NSUInteger currentIndex = [self.VControllers indexOfObject:currentViewController];

    if(currentIndex == 0 || currentIndex %2 == 0)
    {
        UIViewController *nextViewController = [self pageViewController:self.pageController viewControllerAfterViewController:currentViewController];
        viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil];
    }
    else
    {
        UIViewController *previousViewController = [self pageViewController:self.pageController viewControllerBeforeViewController:currentViewController];
        viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil];
    }
    //Now, set the viewControllers property of UIPageViewController
    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];

    return UIPageViewControllerSpineLocationMid;
}
}


 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
 {
int index = [self.VControllers indexOfObject:viewController];

//[self checkViewControllers];
if (index - 1 >= 0)
{
    ind = index;
    return [self.VControllers objectAtIndex:index - 1];
}
ind = index;
return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
int index = [self.VControllers indexOfObject:viewController];
//[self checkViewControllers];
if (index + 1 < [self.VControllers count])
{
    ind = index;
    return [self.VControllers objectAtIndex:index + 1];
}
ind = index;

return nil;
}

我需要的是在显示双视图几次后显示单视图控制器。此功能我无法激活。任何人都可以为此提供指导。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您无法在不更改方向的情况下从显示两个页面更改为仅显示一个页面。

您可以做的是创建自己的容器并自己处理转换。虽然我无法提供自己的任何示例代码,但比看起来更容易。

但是我可以通过一些谷歌搜索来展示一些我能找到的教程:

如果您希望获得翻页效果,可以查看here

通过这种方式,您可以自由地显示您想要的视图控制器数量,并随时更改该数字。这样做甚至可以制作动画。