2012-01-24 56 views
1

我在我的應用程序中使用UIPageViewController和故事板。
當縱向時,我使用以下代碼從一個頁面跳轉到另一個頁面,並且工作正常。ios5 - 頁面視圖控制器翻頁時在橫向

int direction = UIPageViewControllerNavigationDirectionForward; 
if ([self.modelController currentPage] < pagenum) 
{ 
    direction = UIPageViewControllerNavigationDirectionForward; 
} 
else if ([self.modelController currentPage] > pagenum) 
{ 
    direction = UIPageViewControllerNavigationDirectionReverse; 
} 

[self.pageViewController setViewControllers:[NSArray arrayWithObject:[self.modelController viewControllerAtIndex:pagenum storyboard:self.storyboard]] direction:direction animated:YES completion:NULL];  

但是,當我們處於橫向模式時,相同的代碼不起作用。如何在風景中翻頁?

+0

我已經嘗試了很多在我的應用中做這個捲曲效果,但我無法理解UIPageViewController。你能跟我分享一個簡單的演示例子嗎? – Hiren

+0

@CocoaMatters,我沒有演示應用程序......但只是創建一個頁面視圖控制器項目,所有的功能已爲您準備就緒。它也會生成演示頁面 – Satyam

+0

您可以通過[email protected]發送給我嗎? – Hiren

回答

3

如果您在Xcode看看基於頁面的應用程序模板您發現以下UIPageViewControllerDelegate方法:

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation 
{ 
    if (UIInterfaceOrientationIsPortrait(orientation)) { 
     // In portrait orientation: Set the spine position to "min" and the page view controller's view controllers array to contain just one view controller. Setting the spine position to 'UIPageViewControllerSpineLocationMid' in landscape orientation sets the doubleSided property to YES, so set it to NO here. 
     UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0]; 
     NSArray *viewControllers = [NSArray arrayWithObject:currentViewController]; 
     [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; 

     self.pageViewController.doubleSided = NO; 
     return UIPageViewControllerSpineLocationMin; 
    } 

    // In landscape orientation: Set set the spine location to "mid" and the page view controller's view controllers array to contain two view controllers. If the current page is even, set it to contain the current and next view controllers; if it is odd, set the array to contain the previous and current view controllers. 
    DataViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0]; 
    NSArray *viewControllers = nil; 

    NSUInteger indexOfCurrentViewController = [self.modelController indexOfViewController:currentViewController]; 
    if (indexOfCurrentViewController == 0 || indexOfCurrentViewController % 2 == 0) { 
     UIViewController *nextViewController = [self.modelController pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController]; 
     viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil]; 
    } else { 
     UIViewController *previousViewController = [self.modelController pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController]; 
     viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil]; 
    } 
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL]; 


    return UIPageViewControllerSpineLocationMid; 
} 

當從上述方法返回UIPageViewControllerSpineLocationMid,你是在告訴UIPageViewController,它應該預料到兩個UIViewControllers並排顯示(即兩頁)。

這裏的關鍵是你在調用UIPageViewController的setViewControllers時必須傳遞正確數量的UIViewControllers:direction:animated:completion:method。

  • 顯示兩頁?傳遞兩個UIViewControllers。
  • 顯示一個頁面?傳遞一個UIViewController。

你給出的代碼將永遠傳遞多個UIViewController中的UIPageViewController。

如果UIPageViewControllerSpineLocation與您傳遞的UIViewControllers的數量不符,它將會崩潰或者什麼都不做。

讓我知道你是否需要進一步的幫助。

+0

你的傢伙,拯救了我的生命...一輛感謝的卡車是不夠的 – Lolloz89

+0

我們可以在pageviewcontroller中添加兩個以上的視圖控制器,甚至兩個。請指導我。 –

相關問題