8

我的UIPageViewController有一個很大的問題。我想要使​​用部分和子部分在我的應用中呈現內容。所以,我創建「兩化」UIPageViewController實例 - 水平(紅色)和縱向(藍色):將(垂直)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數據源的方法

- (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接收觸摸和滾動兩個方向?

任何幫助,將不勝感激。

+0

感謝您的有用問題的方向。做類似的事情。 – san 2014-10-11 13:52:24

回答

3

我忘了在

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

問題解決了分配我的垂直頁面視圖控制器的dataSourcedelegate性質,一切精美的作品。