UIPageviewController - Change from two page mode to one page mode without changing orientation

折月煮酒 提交于 2019-12-10 09:49:45

问题


I am using UIPageViewController to show two view controllers at a time. After showing a couple of such views, i want to show single view controllers without changing orientation, it is fixed to landscape. I am making in iPad.

Below is the code i am using for this:

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

What i need is to show the single view controllers after shows double views a couple of time. This functionality i am unable to active yet. Can anyone guide for this .

Thanks in advance.


回答1:


You cannot change from showing two pages to showing just one page without changing the orientation.

What you can do is to create your own container and deal with the transitions yourself. Is easier than it looks although I cannot provide with any sample code from myself.

However I can show some tutorials I was able to find with some googling:

  • http://www.cocoanetics.com/2012/04/containing-viewcontrollers/;
  • How does View Controller Containment work in iOS 5?;
  • http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html;
  • http://www.slideshare.net/bobmccune/creating-container-view-controllers#btnNext;
  • http://highoncoding.com/Articles/848_Creating_iPad_Dashboard_Using_UIViewController_Containment.aspx.

If you want to have the page turning effect you may look here.

With this you'll have the freedom to show how many view controllers you want and change that number whenever. And you can even animate when doing that.



来源:https://stackoverflow.com/questions/13776894/uipageviewcontroller-change-from-two-page-mode-to-one-page-mode-without-changi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!