iPhone/iOS UIViewController之UIPageViewController(iOS5 only)

來源:互聯網
上載者:User

一.基本參數及構成

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.