如果您在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的數量不符,它將會崩潰或者什麼都不做。
讓我知道你是否需要進一步的幫助。
我已經嘗試了很多在我的應用中做這個捲曲效果,但我無法理解UIPageViewController。你能跟我分享一個簡單的演示例子嗎? – Hiren
@CocoaMatters,我沒有演示應用程序......但只是創建一個頁面視圖控制器項目,所有的功能已爲您準備就緒。它也會生成演示頁面 – Satyam
您可以通過[email protected]發送給我嗎? – Hiren