iPhone/iOS UIViewController之UIPageViewController(iOS5 only)

一.基本参数及构成


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







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值