UIPageViewController setViewControllers在方向更改后不会更改

时间:2012-08-23 20:10:19

标签: objective-c ios uipageviewcontroller

我可以使用setViewControllers方法以编程方式更改页面,而无需更改设备方向(从纵向横向,反之亦然)。但在更改设备方向后,setViewControllers不会更改页面。

spineLocationForInterfaceOrientation实施:

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
                   spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    J2HPageDataViewController *currentViewController = [self.viewControllers objectAtIndex:0];

    NSArray *viewControllers = nil;

    if (UIInterfaceOrientationIsPortrait(orientation))
    {
        viewControllers = @[currentViewController];

        [self setViewControllers:viewControllers
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:YES
                      completion:NULL];

        self.doubleSided = NO;

        [self.navigationController setNavigationBarHidden:NO animated:YES];

        return UIPageViewControllerSpineLocationMin;
    }


    NSUInteger indexOfCurrentViewController = [self.modelController indexOfViewController:currentViewController];

    if (indexOfCurrentViewController == 0 || indexOfCurrentViewController % 2 == 0)
    {
        UIViewController *nextViewController = [self.modelController pageViewController:self
                                                      viewControllerAfterViewController:currentViewController];
        viewControllers = @[currentViewController, nextViewController];
    }
    else
    {
        UIViewController *previousViewController = [self.modelController pageViewController:self
                                                         viewControllerBeforeViewController:currentViewController];
        viewControllers = @[previousViewController, currentViewController];
    }

    [self setViewControllers:viewControllers
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:YES
                  completion:NULL];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

    return UIPageViewControllerSpineLocationMid;
}

setViewControllers实现:

- (void)selectPageByPageEntity:(J2HPageEntity *)pageEntity
{
    J2HPageImageEntity *pageLargeImageEntity = pageEntity.largeImage;

    NSUInteger index = [self.modelController indexOfImageEntity:pageLargeImageEntity];

    J2HPageDataViewController *currentViewController = [self.viewControllers objectAtIndex:0];
    NSUInteger indexOfCurrentViewController = [self.modelController indexOfViewController:currentViewController];

    if (index == indexOfCurrentViewController)
    {
        return;
    }

    if ([J2HApplication isLandscapeOrientation])
    {
        if (index % 2 == 1 && (index - 1) == indexOfCurrentViewController)
        {
            return;
        }
        if ((index == 0 || index % 2 == 0) && (index + 1) == indexOfCurrentViewController)
        {
            return;
        }
    }

    UIPageViewControllerNavigationDirection direction;
    if (indexOfCurrentViewController > index)
    {
        direction = UIPageViewControllerNavigationDirectionReverse;
    }
    else
    {
        direction = UIPageViewControllerNavigationDirectionForward;
    }

    J2HPageDataViewController *selectedViewController
        = [self.modelController viewControllerAtIndex:index
                                           storyboard:self.storyboard];

    NSArray *viewControllers = nil;
    if ([J2HApplication isLandscapeOrientation])
    {
        if (index == 0 || index % 2 == 0)
        {
            UIViewController *nextViewController
                = [self.modelController pageViewController:self
                         viewControllerAfterViewController:selectedViewController];

            viewControllers = @[selectedViewController, nextViewController];
        }
        else
        {
            UIViewController *previousViewController
                = [self.modelController pageViewController:self
                        viewControllerBeforeViewController:selectedViewController];

            viewControllers = @[previousViewController, selectedViewController];
        }
    }
    else
    {
        viewControllers = @[selectedViewController];
    }

    [self setViewControllers:viewControllers
                   direction:direction
                    animated:YES
                  completion:NULL];

    [self playSound:@"PageFlip" extension:@"mp3"];
}

0 个答案:

没有答案