将(垂直)UIPageViewController嵌套在另一个(水平)UIPageViewcontroller中 [英] Nesting a (vertical) UIPageViewController inside another (horizontal) UIPageViewcontroller

查看:129
本文介绍了将(垂直)UIPageViewController嵌套在另一个(水平)UIPageViewcontroller中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





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



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



它们在分开时都有效但是当我将纵向 UIPageViewController 放在水平位置时,垂直方向停止工作



我的代码如下所示:






水平 UIPageViewController 创建

  self.mainPageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizo​​ntal选项:@ {UIPageViewControllerOptionInterPageSpacingKey: [NSNumber numberWithFloat:0]}]; 
self.mainPageViewController.dataSource = self;
self.mainPageViewController.delegate = self;

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

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

@ {@v:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@3vofType:@pdf]],
@h: [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@3hofType:@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]密码:nil];
PDFDocument * hdoc = [[PDFDocument alloc] initWithPDFFileURL:self.pdfDocsURLs [0] [@h]密码:nil];
PDFSinglePageViewController * svc = [[PDFSinglePageViewController alloc] initWithVerticalPDF:vdoc horizo​​ntalPDF:hdoc page:1];
[pvc setViewControllers:@ [svc]方向:UIPageViewControllerNavigationDirectionForward动画:NO completion:nil];

[self.mainPageViewController setViewControllers:@ [pvc]方向:UIPageViewControllerNavigationDirectionForward动画:NO completion:nil];

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






UIPageViewController dataSource方法

   - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController: (UIViewController *)viewController {
if(pageViewController == self.mainPageViewController){// if horizo​​ntal
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 horizo​​ntalPDF:hdoc page:1];
[pvc setViewControllers:@ [svc]方向:UIPageViewControllerNavigationDirectionForward动画:NO completion:nil];
返回pvc;
} else {//如果垂直 - 下面的代码从未执行过
PDFSinglePageViewController * ovc =(PDFSinglePageViewController *)viewController;
NSUInteger nop = 0;
if(UIInterfaceOrientationIsPortrait(ovc.interfaceOrientation))nop = ovc.verticalDoc.numberOfPages;
else if(UIInterfaceOrientationIsLandscape(ovc.interfaceOrientation))nop = ovc.horizo​​ntalDoc.numberOfPages;
if(ovc.page == nop)return nil;
PDFSinglePageViewController * svc = [[PDFSinglePageViewController alloc] initWithVerticalPDF:ovc.verticalDoc horizo​​ntalPDF:ovc.horizo​​ntalDoc page:ovc.page + 1];
返回svc;
}
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if(pageViewController == self.mainPageViewController ){//如果水平
UIIndexedPageViewController * ivc =(UIIndexedPageViewController *)viewController;
if(ivc.index == 1)返回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 horizo​​ntalPDF:hdoc page:1];
[pvc setViewControllers:@ [svc]方向:UIPageViewControllerNavigationDirectionForward动画:NO completion:nil];
返回pvc;
} else {//是垂直的 - 下面的代码永远不会执行
PDFSinglePageViewController * ovc =(PDFSinglePageViewController *)viewController;
NSUInteger nop = 0;
if(UIInterfaceOrientationIsPortrait(ovc.interfaceOrientation))nop = ovc.verticalDoc.numberOfPages;
else if(UIInterfaceOrientationIsLandscape(ovc.interfaceOrientation))nop = ovc.horizo​​ntalDoc.numberOfPages;
if(ovc.page == 1)return nil;
PDFSinglePageViewController * svc = [[PDFSinglePageViewController alloc] initWithVerticalPDF:ovc.verticalDoc horizo​​ntalPDF:ovc.horizo​​ntalDoc page:ovc.page-1];
返回svc;
}
}






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






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



任何帮助都将不胜感激。

解决方案

我忘了在
$ b中分配 dataSource 委托我的垂直页面视图控制器的属性$ b


  • -pageViewController:viewControllerBeforeViewController:

  • -pageViewController: viewControllerAfterViewController:



问题解决后,一切运行良好。


I have a big problem with my UIPageViewController. I want to present content in my app using sections and sub-sections. So, I have created "two" instances of UIPageViewController - horizontal (red) and vertical (blue):

Earlier I said I have created "two" instances - it is not quite true - there can be tens of instances, but only 2 are presented at the same time, you know what I mean. Both controllers have transitionStyle set to UIPageViewControllerTransitionStyleScroll.

The red UIPageViewController is responsible for horizontal scrolling between sections and blue are responsible for vertical scrolling.

They both work when separated but when I put vertical UIPageViewController in horizontal, the vertical one stops working.

My code is presented below:


Horizontal UIPageViewController creation

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];

Just to be clear, UIIndexedPageViewController is a subclass of UIPVC with extra NSUInteger index property. Its implementation is empty - it doesn't overwrite any methods.


UIPageViewController dataSource methods

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


So it seems that horizontal UIPageViewController doesn't allow the vertical one to even receive pan gesture recognizers.


My question is... Is there a way to allow the vertical UIPageViewController to receive touches and scroll in both directions?

Any help would be appreciated.

解决方案

I forgot to assign dataSource and delegate properties of my vertical page view controllers in

  • -pageViewController:viewControllerBeforeViewController:
  • -pageViewController:viewControllerAfterViewController:

Problem solved, everything works beautifully.

这篇关于将(垂直)UIPageViewController嵌套在另一个(水平)UIPageViewcontroller中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆