一.基本參數及構成
Datasource提供資料來源
delegate跟蹤手勢和裝置方向變化
viewControllers顯示在當前螢幕的view Controller,最多2個,這個是唯讀,設定的化要調用相應的setViewControllers
guesturRecognizers:手勢識別,可以將相關手勢添加到更大的view中去,比方說pageViewController.view的parentView
二.建立
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; //建立時需要設定書脊的位置,有效參數有max,min,mid,其中mid將會需要設定2頁 NSDictionary * options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt:UIPageViewControllerSpineLocationMid] forKey:UIPageViewControllerOptionSpineLocationKey]; //transitionStyle只有一種;navigationOrientation表示controller的方向 UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options]; self.viewController = [[[FCDemoViewController alloc] initWithNibName:@"FCDemoViewController" bundle:nil] autorelease]; FirstView *first= [[[FirstView alloc] initWithNibName:@"FirstView" bundle:nil] autorelease]; //設定initial content controller 這個是必須的,而且設定的array必鬚根據書脊的位置,如果是2頁必須是2個controller //,如果沒有正確設定initial controller,就會拋出一個shouldAutorotateToInterfaceOrientation的錯誤 [pageViewController setViewControllers:[NSArray arrayWithObjects:self.viewController,first, nil] direction:UIPageViewControllerNavigationOrientationHorizontal animated:YES completion:nil]; DemoDataSource *snd= [[DemoDataSource alloc]init]; pageViewController.dataSource=snd; DemoDelegate *del= [[DemoDelegate alloc]init]; pageViewController.delegate=del; pageViewController.view.gestureRecognizers=pageViewController.gestureRecognizers; self.window.rootViewController =pageViewController;
相關的設定和參數基本都已經設定好了,相關解釋在注釋中,
值得注意的是設定了datasource,此外,datasource最好是一個直接繼承NSObject的自訂類。
此外還需要設定initial content就是設定viewControllers這個是必須的,因為這個是第一頁內容的顯示。
三.設定內容
1.在datasource中設定
聲明類:
@interface DemoDataSource : NSObject<UIPageViewControllerDataSource>
實現方法:
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{ Second *cons= [[[Second alloc] initWithNibName:@"Second" bundle:nil] autorelease]; return cons;}-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{ Second *cons= [[[Second alloc] initWithNibName:@"Second" bundle:nil] autorelease]; return cons;}
值得一提的是,如果你是要相反方向翻書的話,就是從左邊往右邊是下一頁,則提供內容的2個方法需要顛倒一下提供的viewController就可以了
2.直接設定initial view controller
這個一般是用於跳轉的方式,比方說從某一頁跳轉到另外某一頁。
四.代理方法
-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{}//提供方向變化後,書脊位置的調整,一般是橫屏是mid,分2頁,豎屏min,分一頁-(UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation{ if(UIInterfaceOrientationIsLandscape(orientation)){ return UIPageViewControllerSpineLocationMid; }else{ return UIPageViewControllerSpineLocationMin; }}