将(垂直)UIPageViewController嵌套在另一个(水平)UIPageViewcontroller中

时间:2013-02-23 11:05:45

标签: iphone objective-c uiviewcontroller uipageviewcontroller nested

我的UIPageViewController存在很大问题。我想使用部分和子部分在我的应用程序中显示内容。所以,我创建了UIPageViewController“两个”实例 - 水平(红色)和垂直(蓝色):

scheme

之前我曾说过我已经创建了“两个”实例 - 这不是真的 - 可能有几十个实例,但同时只有两个实例,你知道我的意思。两个控制器都将transitionStyle设置为UIPageViewControllerTransitionStyleScroll

红色UIPageViewController负责部分之间的水平滚动,蓝色负责垂直滚动。

它们在分开时都可以工作,但是当我将垂直UIPageViewController放在水平方向时,垂直方向会停止工作

我的代码如下:


横向UIPageViewController创建

self.mainPageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionInterPageSpacingKey:[NSNumber numberWithFloat:0]}];
self.mainPageViewController.dataSource = self;
self.mainPageViewController.delegate = self;

self.pdfDocsURLs = @[ @{@"v":[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1v" ofType:@"pdf"]],
                        @"h":[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1h" ofType:@"pdf"]]},

                      @{@"v":[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"2v" ofType:@"pdf"]],
                        @"h":[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"2h" ofType:@"pdf"]]},

                      @{@"v":[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"3v" ofType:@"pdf"]],
                        @"h":[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"3h" ofType:@"pdf"]]}];

UIIndexedPageViewController *pvc = [[UIIndexedPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:@{UIPageViewControllerOptionInterPageSpacingKey:[NSNumber numberWithFloat:0]}];
pvc.index = 1;
PDFDocument *vdoc = [[PDFDocument alloc] initWithPDFFileURL:self.pdfDocsURLs[0][@"v"] password:nil];
PDFDocument *hdoc = [[PDFDocument alloc] initWithPDFFileURL:self.pdfDocsURLs[0][@"h"] password:nil];
PDFSinglePageViewController *svc = [[PDFSinglePageViewController alloc] initWithVerticalPDF:vdoc horizontalPDF:hdoc page:1];
[pvc setViewControllers:@[svc] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

[self.mainPageViewController setViewControllers:@[pvc] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

为了清楚起见,UIIndexedPageViewController是UIPVC的子类,具有额外的NSUInteger index属性。它的实现是空的 - 它不会覆盖任何方法。


UIPageViewController dataSource方法

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if(pageViewController == self.mainPageViewController) { // if horizontal
        UIIndexedPageViewController *ivc = (UIIndexedPageViewController *)viewController;
        if(ivc.index == self.pdfDocsURLs.count) return nil;
        UIIndexedPageViewController *pvc = [[UIIndexedPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:@{UIPageViewControllerOptionInterPageSpacingKey:[NSNumber numberWithFloat:0]}];
        pvc.index = ivc.index+1;
        PDFDocument *vdoc = [[PDFDocument alloc] initWithPDFFileURL:self.pdfDocsURLs[ivc.index][@"v"] password:nil];
        PDFDocument *hdoc = [[PDFDocument alloc] initWithPDFFileURL:self.pdfDocsURLs[ivc.index][@"h"] password:nil];
        PDFSinglePageViewController *svc = [[PDFSinglePageViewController alloc] initWithVerticalPDF:vdoc horizontalPDF:hdoc page:1];
        [pvc setViewControllers:@[svc] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
        return pvc;
    } else { // if vertical - THE CODE BELOW IS NEVER EXECUTED
        PDFSinglePageViewController *ovc = (PDFSinglePageViewController *)viewController;
        NSUInteger nop = 0;
        if(UIInterfaceOrientationIsPortrait(ovc.interfaceOrientation)) nop = ovc.verticalDoc.numberOfPages;
        else if(UIInterfaceOrientationIsLandscape(ovc.interfaceOrientation)) nop = ovc.horizontalDoc.numberOfPages;
        if(ovc.page == nop) return nil;
        PDFSinglePageViewController *svc = [[PDFSinglePageViewController alloc] initWithVerticalPDF:ovc.verticalDoc horizontalPDF:ovc.horizontalDoc page:ovc.page+1];
        return svc;
    }
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if(pageViewController == self.mainPageViewController) { // if horizontal
        UIIndexedPageViewController *ivc = (UIIndexedPageViewController *)viewController;
        if(ivc.index == 1) return nil;
        UIIndexedPageViewController *pvc = [[UIIndexedPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:@{UIPageViewControllerOptionInterPageSpacingKey:[NSNumber numberWithFloat:0]}];
        pvc.index = ivc.index-1;
        PDFDocument *vdoc = [[PDFDocument alloc] initWithPDFFileURL:self.pdfDocsURLs[ivc.index-2][@"v"] password:nil];
        PDFDocument *hdoc = [[PDFDocument alloc] initWithPDFFileURL:self.pdfDocsURLs[ivc.index-2][@"h"] password:nil];
        PDFSinglePageViewController *svc = [[PDFSinglePageViewController alloc] initWithVerticalPDF:vdoc horizontalPDF:hdoc page:1];
        [pvc setViewControllers:@[svc] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
        return pvc;
    } else { // is vertical - THE CODE BELOW IS NEVER EXECUTED
        PDFSinglePageViewController *ovc = (PDFSinglePageViewController *)viewController;
        NSUInteger nop = 0;
        if(UIInterfaceOrientationIsPortrait(ovc.interfaceOrientation)) nop = ovc.verticalDoc.numberOfPages;
        else if(UIInterfaceOrientationIsLandscape(ovc.interfaceOrientation)) nop = ovc.horizontalDoc.numberOfPages;
        if(ovc.page == 1) return nil;
        PDFSinglePageViewController *svc = [[PDFSinglePageViewController alloc] initWithVerticalPDF:ovc.verticalDoc horizontalPDF:ovc.horizontalDoc page:ovc.page-1];
        return svc;
    }
}

因此水平UIPageViewController似乎不允许垂直的人接收平移手势识别器。


我的问题是...... 有没有办法让垂直UIPageViewController接收触摸并向两个方向滚动?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

我忘了在

中分配垂直网页浏览控制器的dataSourcedelegate属性
  • -pageViewController:viewControllerBeforeViewController:
  • -pageViewController:viewControllerAfterViewController:

问题解决了,一切都很美好。