UIPageViewController是App中常用的控制器。它提供了一种分页效果来显示其childController的View。用户可以通过手势像翻书一样切换页面。
切换页面时看起来是连续的,但静止状态下UIPageViewController同时只有一个childViewController。它的导航效果是通过替换childController实现的。
这是一种非常有效的设计:无论是三两个还上千个页面,用户的翻页与导航处理都是无差别的。因为这些页面都是即时创建的,每个页面只有当你浏览它的时候才会存在。这种设计显然苹果更多地考虑了内存的问题和通用性。
UIPageViewController替我们解决了页面导航、childController生命周期管理、 平滑过渡动画(index不相邻页面切换时)等问题。
下面做了简单是学习!

//
//  ViewController.m
//  DemoTest

#import "ViewController.h"
#import "PageChildViewController.h"

@interface ViewController ()<UIPageViewControllerDelegate, UIPageViewControllerDataSource>

@property (nonatomic, strong) UIPageViewController * pageViewController;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self setUpPageViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/** UIPageViewController */
- (void)setUpPageViewController {
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
}

//翻页效果
//UIPageViewControllerTransitionStylePageCurl
//滚动效果
//UIPageViewControllerTransitionStyleScroll

//效果方向
//UIPageViewControllerNavigationOrientationHorizontal
//UIPageViewControllerNavigationOrientationVertical

//从左往右(或从下往上)
//UIPageViewControllerNavigationDirectionForward,
//从左往右(或从下往上)
//UIPageViewControllerNavigationDirectionReverse

//options: 这个参数是可选的,传入的是对UIPageViewController的一些配置组成的字典,不过这个参数只能以UIPageViewControllerOptionSpineLocationKey和UIPageViewControllerOptionInterPageSpacingKey这两个key组成的字典.
//1) UIPageViewControllerOptionSpineLocationKey 这个key只有在style是翻书效果UIPageViewControllerTransitionStylePageCurl的时候才有作用, 它定义的是书脊的位置,值对应着UIPageViewControllerSpineLocation这个枚举项,不要定义错了哦.
//2) UIPageViewControllerOptionInterPageSpacingKey这个key只有在style是UIScrollView滚动效果UIPageViewControllerTransitionStyleScroll的时候才有作用, 它定义的是两个页面之间的间距(默认间距是0).
//UIPageViewControllerOptionInterPageSpacingKey - 两者之间间距
//UIPageViewControllerOptionSpineLocationKey - 书脊的位置
//除了初始化方法系统还提供了一个属性
//
//@property (nonatomic, getter=isDoubleSided) BOOL doubleSided; // Default is 'NO'.
//
//这个属性默认为NO,如果我们当前屏幕仅展示一个页面那么不用设置这个属性,如果设置了UIPageViewControllerSpineLocationMid这个选项,效果是翻开的书这样屏幕展示的就是两个页面,这个属性就必须设置为YES了.
//
//此外还有一个重要方法:
//
//- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers
//                 direction:(UIPageViewControllerNavigationDirection)direction
//                  animated:(BOOL)animated
//                completion:(void (^ __nullable)(BOOL finished))completion;
//
//这个方法是设置UIPageViewController初始显示的页面,如果doubleSided设为YES了,那么viewControllers这个参数至少包含两个页面.

- (UIPageViewController *)pageViewController {
    if (!_pageViewController) {
        NSDictionary *options = @{UIPageViewControllerOptionInterPageSpacingKey : @(20)};
//        NSDictionary *options = @{UIPageViewControllerOptionSpineLocationKey : @(UIPageViewControllerSpineLocationMin)};
        self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:(UIPageViewControllerTransitionStyleScroll) navigationOrientation:(UIPageViewControllerNavigationOrientationHorizontal) options:options];
        _pageViewController.delegate = self;
        _pageViewController.dataSource = self;

//appearance 预设一个类的设置,不包括tintColor;
//        UIPageControl *pageControl = [UIPageControl appearance];
//        pageControl.pageIndicatorTintColor = [UIColor greenColor];
//        pageControl.currentPageIndicatorTintColor = [UIColor redColor];
//        pageControl.backgroundColor = [UIColor whiteColor];
        [_pageViewController setViewControllers:@[[self viewControllerAtIndex:0]] direction:(UIPageViewControllerNavigationDirectionForward) animated:YES completion:nil];
        _pageViewController.view.frame = self.view.bounds;
    }
    return _pageViewController;
}
#pragma mark - delegate
/** 开始滚动时 */
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
    NSInteger index = [(PageChildViewController *)pendingViewControllers.firstObject pageIndex];
    NSLog(@"-=-=-=-=-=000000000-=-=-==-=-==%ld", index);
}
/** 结束滚动时 */
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    NSInteger index = [(PageChildViewController *)previousViewControllers.firstObject pageIndex];
    NSLog(@"-=-=-=-=-=111111111-=-=-==-=-==%ld", index);
}
///** 横竖屏变化 */
//- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation {
//    
//}
///** 设置UIPageViewController支持的屏幕旋转类型 */
//- (UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController {
//    
//}
///** 设置应用程序当前的屏幕的方向 */
//- (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController {
//    
//}
#pragma mark - dataSource
/** 返回前一个页面,nil时不滚动 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSInteger index = [(PageChildViewController *)viewController pageIndex];
    if (index == 0) {
        return nil;
    }else {
        index--;
        return [self viewControllerAtIndex:index];
    }
}
/** 返回后一个页面,nil时不滚动 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSInteger index = [(PageChildViewController *)viewController pageIndex];
    if (index == 3) {
        return nil;
    }else {
        index++;
        return [self viewControllerAtIndex:index];
    }
}

///** Item总数 */
//- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
//    return 4;
//}
///** 返回默认展示的页面index */
//- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
//    return 0;
//}

- (UIViewController *)viewControllerAtIndex:(NSInteger)index {
    PageChildViewController *viewController = [[PageChildViewController alloc] init];
    viewController.pageIndex = index;
    viewController.view.backgroundColor = [self LPCColorRandom];
    return viewController;
}

- (UIColor *)LPCColorRandom {
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

@end

iOS UIPageViewController的更多相关文章

  1. iOS UIPageViewController缺陷

    为什么弃用UIPageViewController?问题1:设置UIPageViewController为UIPageViewControllerTransitionStyleScroll且调用set ...

  2. iOS:UIPageViewController翻页控制器控件详细介绍

    翻页控制器控件:UIPageViewController 介绍: 1.它是为我们提供了一种类似翻书效果的一种控件.我们可以通过使用UIPageViewController控件,来完成类似图书一样的翻页 ...

  3. Learn how to Use UIPageViewController in iOS

    下面学习内容来自国外的IOS学习网站:The AppGuruz: UIPageViewController in iOS  也许需要FQ哦 认真做一遍上面入门UIPageController的教程,然 ...

  4. iOS 5 :一个UIPageViewController程序示例

    原文:http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_UIPageViewController_Application 在Xco ...

  5. iOS 资源大全

    这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...

  6. iOS “智慧气象”APP中用到的第三方框架汇总

    “智慧气象”是我最近在公司接手的项目,已经完成最新版本的更新并上架,在此分享下其中用到的第三方框架的使用. 应用地址:APP商店搜索“智慧气象” MJRefresh(下拉刷新)业界知名下拉刷新框架就不 ...

  7. IOS中文版资源库

    Swift 语言写成的项目会被标记为  ★ ,AppleWatch 的项目则会被标记为 ▲. [转自]https://github.com/jobbole/awesome-ios-cn#librari ...

  8. ios开发 <AppName>-Prefix.pch文件的用法详解

    我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个 ...

  9. iOS 中关于ViewController总结

    以前写程序时,经常被旋转问题弄的头疼,今天为了解决这个问题,偶然看到了苹果官方文档 View Controller Programming Guide for iOS. 这才发现这个必读的资料!以前许 ...

随机推荐

  1. 准备 LVM Volume Provider - 每天5分钟玩转 OpenStack(49)

    Cinder 真正负责 Volume 管理的组件是 volume provider. Cinder 支持多种 volume provider,LVM 是默认的 volume provider.Devs ...

  2. 窥探Swift之需要注意的基本运算符和高级运算符

    之前更新了一段时间有关Swift语言的博客,连续更新了有6.7篇的样子.期间间更新了一些iOS开发中SQLite.CollectionViewController以及ReactiveCocoa的一些东 ...

  3. windows配置xhprof,PHP性能分析工具

    本来以为配置这么一个工具不会费很大的力气,后面发现完全不是. 一.小插曲 早上显示电脑不能显示虚拟目录下的所有域名,但是能打开localhost,数据库连接也不行了.这个问题纠缠了我一个上午.对了还有 ...

  4. 深入seajs源码系列一

    简述 前端开发模块化已经是大势所趋,目前模块化的规范有很多,众所周知的有commonJS,Module/Wrappings和AMD等,而且ES6也着手开始制定模块化机制的实现.类似于c/c++的inc ...

  5. (四)WebGIS中通过行列号来换算出多种瓦片的URL 之离线地图

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.前言 在前面我花了两个篇幅来讲解行列号的获取,也解释了为什么要获取行 ...

  6. 1.Struts2简介和Struts2开发环境搭建

    一.Struts2简介: 1.什么是Struts2? 著名的SSH三大框架分别为:表现层(Struts).业务逻辑层(Spring),持久化层(Hibernate). Struts2是在WebWork ...

  7. OpenCV2:Mat

    1.Mat基础 在计算机内存中,数字图像是已矩阵的形式保存的.OpenCV2中,数据结构Mat是保存图像像素信息的矩阵,它主要包含两部分:矩阵头和一个指向像素数据的矩阵指针. 矩阵头主要包含,矩阵尺寸 ...

  8. 为.NET搭建Linux的开发环境,鄙视那些将简单事情复杂化的人

    写在前面的吐槽 原本跨平台开发很容易的事情, 很多人把它弄得很麻烦,给外人的感觉:你们.NET跨平台开发好不成熟,好麻烦哦. ..................................... ...

  9. 4.DB Initialization(数据库初始化)[EF Code-First系列]

    前面的例子中,我们已经看到了Code-First自动为我们创建数据库的例子. 这里我们将要学习的是,当初始化的时候,Code-First是怎么决定数据库的名字和服务的呢??? 下面的图,解释了这一切! ...

  10. centos6.x 安装pylucene (20161027改)

    一.说明 安装环境 centos6.6 (64位) python2.7.10 (升级系统默认python版本的方法参见在CentOS 6.5上安装python2.7) 约定 工作目录假定为当前用户的H ...