美文网首页
UIPageViewController

UIPageViewController

作者: 霸_霸霸 | 来源:发表于2018-04-09 16:44 被阅读54次

    UIPageViewController的使用

    1. 创建属性

    创建一个UIPageViewController对象

    @property (nonatomic, strong)UIPageViewController *pageVC;
    

    创建一个数组用来存放要在pageViewController中展示的所有的控制器

    @property (nonatomic, copy)NSArray *dataSource;
    

    注: 如果你想在顶部加上几个按钮用以控制页面的跳转, 就需要创建以下两个属性
    创建一个currentIndex作为当前是第几页的标识

    @property (nonatomic, assign)NSInteger currentIndex;
    

    定义一个静态变量temp_index来存放当前的是第几页的标识, 这个和上面的currentIndex属性不一样, temp_index是用来给currentIndex赋值的, 类似于一个侦察兵, 只有当temp_index确定控制器发生变化, 才会将temp_index的值赋给currentIndex

    static NSInteger temp_index;
    

    2.懒加载

    - (NSArray *)dataSource {
        if (!_dataSource) {
            _dataSource = [NSArray arrayWithObjects:[LucyViewController new],[LucyViewController new],[LucyViewController new], nil];
        }
        return _dataSource;
    }
    

    这里我放了三个LucyViewController(其实就是ContentViewController), 仅仅用来展示

    - (UIPageViewController *)pageVC {
        if(!_pageVC) {
            NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:10] forKey:UIPageViewControllerOptionInterPageSpacingKey];
            _pageVC = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
            _pageVC.view.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64);
            _pageVC.delegate=self;
            _pageVC.dataSource=self;
            //因为UIPageViewController也是继承自UIViewController, 所以采用addChildViewController方法
            [self addChildViewController:_pageVC];
            [self.view addSubview:_pageVC.view];
        }
        return _pageVC;
    }
    
    

    其中需要注意的是这个方法

    
    _pageVC = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
    
    

    这个方法有三个参数:
    1.style
    这个参数是UIPageViewController翻页的过渡样式,系统提供了两种过度样式,分别是
    UIPageViewControllerTransitionStylePageCurl : 卷曲样式类似翻书效果
    UIPageViewControllerTransitionStyleScroll : UIScrollView滚动效果
    2.navigationOrientation
    这个参数是UIPageViewController导航方向,系统提供了两种方式,分别是
    UIPageViewControllerNavigationOrientationHorizontal : 水平导航方式
    UIPageViewControllerNavigationOrientationVertical : 垂直导航方式
    3.options
    这个参数是可选的,传入的是对UIPageViewController的一些配置组成的字典,不过这个参数只能以UIPageViewControllerOptionSpineLocationKey和UIPageViewControllerOptionInterPageSpacingKey这两个key组成的字典.
    a). UIPageViewControllerOptionSpineLocationKey 这个key只有在style是翻书效果UIPageViewControllerTransitionStylePageCurl的时候才有作用, 它定义的是书脊的位置,值对应着UIPageViewControllerSpineLocation这个枚举项,不要定义错了哦.
    b). UIPageViewControllerOptionInterPageSpacingKey这个key只有在style是UIScrollView滚动效果UIPageViewControllerTransitionStyleScroll的时候才有作用, 它定义的是两个页面之间的间距(默认间距是0).

    3.初始化

    - (void)viewDidLoad {
        [super viewDidLoad];
        //VCAtIndex是自定义的方法, 在后面
        LucyViewController *lucyVC = [self VCAtIndex:0];
        NSArray *vcs = [NSArray arrayWithObjects:lucyVC, nil];
        [self.pageVC setViewControllers:vcs direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
    }
    

    4. 代理方法

    UIPageViewControllerDataSource包含两个required的方法

    //这个方法表示的是, 当前控制器的 前 一个控制器是什么, 我们要告诉pageVC
    - (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
    //这个方法表示的是, 当前控制器的 后 一个控制器是什么, 我们也要告诉pageVC
    - (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
    

    可做如下实现:

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
        NSInteger index = [self indexOfVC:viewController];
        if (index == 0 || index == NSNotFound) {
            return nil;
        }else {
            index --;
            return [self VCAtIndex:index];
        }
    }
    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
        NSInteger index = [self indexOfVC:viewController];
        if (index == self.dataSource.count - 1 || index == NSNotFound) {
            return nil;
        }else {
            index ++;
            return [self VCAtIndex:index];
        }
    }
    

    5.两个简单的工具方法

    //通过当前的viewController得知当前的index
    - (NSInteger)indexOfVC:(UIViewController *)vc {
        return [self.dataSource indexOfObject:vc];
    }
    //通过当前的index得知当前的viewController
    - (UIViewController *)VCAtIndex:(NSInteger)index {
        return self.dataSource[index];
    }
    

    6.如果你不打算通过在顶部的按钮来控制页面的滚动, 那就不需要实现下面两个UIPageViewControllerDelegate方法

    /*
     如何获取下一个控制器的索引, 在上面两个数据源方法里无法获取准确的索引, 而应该在下面这个方法里获取:
     pendingViewControllers里包含的就是 即将 显示的那个控制器, 是一个数组, 如果是单页显示的话, 其中只有一个元素
     */
    - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
        UIViewController *firstVc = pendingViewControllers.firstObject;
        NSInteger index = [self.dataSource indexOfObject:firstVc];
        temp_index = index;
    }
    /*
     这里是确定跳转到下一个vc了
     */
    - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
        
        if (completed) {
            //已经跳转完成
            self.currentIndex = temp_index;
        }
    }
    

    顶部的三个按钮对应的拖进来的方法

    - (IBAction)clickFirstButton:(UIButton *)sender {
        self.currentIndex = 0;
        [self.pageVC setViewControllers:@[self.dataSource[0]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
    }
    - (IBAction)clickSecondButton:(UIButton *)sender {
        if (self.currentIndex > 1) {
            [self.pageVC setViewControllers:@[self.dataSource[1]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
        }else {
            [self.pageVC setViewControllers:@[self.dataSource[1]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
        }
        self.currentIndex = 1;
    }
    - (IBAction)clickThirdButton:(UIButton *)sender {
        [self.pageVC setViewControllers:@[self.dataSource[2]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
        self.currentIndex = 2;
    }
    

    相关文章

      网友评论

          本文标题:UIPageViewController

          本文链接:https://www.haomeiwen.com/subject/kkbihftx.html