UIPageViewController的使用

最近做阅读页的翻页效果的时候,大量的使用了UIPageViewController,这里记录一下,方便以后使用备忘。

打开这个类的api,发现这个类还是比较简单的,主要是一个初始化方法,一个设置当前页面的方法,还有两个代理,其他的属性大部分都是只读属性,这些属性一般只能在初始化方法中设置。

一. 初始化

- (instancetype)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(nullable NSDictionary<UIPageViewControllerOptionsKey, id> *)options

主要有三个初始化参数,这里介绍一下:

  • style,枚举类型,控制翻页效果
typedef NS_ENUM(NSInteger, UIPageViewControllerTransitionStyle) {
// 仿真翻页,卷曲样式类似翻书效果
    UIPageViewControllerTransitionStylePageCurl = 0, // Navigate between views via a page curl transition.
// 滑动翻页,UIScrollView滚动效果
    UIPageViewControllerTransitionStyleScroll = 1 // Navigate between views by scrolling.
};
  • navigationOrientation,导航方向,枚举类型,水平方向或者垂直方向
  • options,配置,是一个字典,只有两个Key
  1. UIPageViewControllerOptionSpineLocationKey,定义的是仿真翻页的时候模拟书脊的位置,只有在style为UIPageViewControllerTransitionStylePageCurl是生效
typedef NS_ENUM(NSInteger, UIPageViewControllerSpineLocation) {
// 非UIPageViewControllerTransitionStylePageCurl时默认值
    UIPageViewControllerSpineLocationNone = 0, // Returned if 'spineLocation' is queried when 'transitionStyle' is not 'UIPageViewControllerTransitionStylePageCurl'.
// 书脊位置在最左边,UIPageViewControllerTransitionStylePageCurl仿真翻页时的默认值
    UIPageViewControllerSpineLocationMin = 1,  // Requires one view controller.
// 书脊位置在中间,设置当前页面的时候需要两个控制器
    UIPageViewControllerSpineLocationMid = 2,  // Requires two view controllers.
// 书脊位置在最右边
    UIPageViewControllerSpineLocationMax = 3   // Requires one view controller.
};   // Only pertains to 'UIPageViewControllerTransitionStylePageCurl'.

2.UIPageViewControllerOptionInterPageSpacingKey,定义的事滚动翻页的时候两个页面的间距,只有style为UIPageViewControllerTransitionStyleScroll时才生效,默认值为0

二. 设置当前页面

在创建pageViewController之后,还需要设置其当前页面,这就需要用到下面这个方法

// Set visible view controllers, optionally with animation. Array should only include view controllers that will be visible after the animation has completed.
// For transition style 'UIPageViewControllerTransitionStylePageCurl', if 'doubleSided' is 'YES' and the spine location is not 'UIPageViewControllerSpineLocationMid', two view controllers must be included, as the latter view controller is used as the back.
- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;

这个方法用于设置当前可见的控制器,可以选择时候有动画,

  • viewControllers, 将要显示的控制器数组,只能是包含controllers的数组。
  • direction 切换动画的方向
  • animated bool值,是否显示动画
  • completion 切换完成后的回调

当style为UIPageViewControllerTransitionStylePageCurl仿真翻页,UIPageViewControllerOptionSpineLocationKey值为UIPageViewControllerSpineLocationMid书脊在中间位置,并且属性doubleSidedYES时,数组里面必须包含两个控制器,否则会崩溃。

  • doubleSided 属性
// Whether client content appears on both sides of each page. If 'NO', content on page front will partially show through back.
// If 'UIPageViewControllerSpineLocationMid' is set, 'doubleSided' is set to 'YES'. Setting 'NO' when spine location is mid results in an exception.
@property (nonatomic, getter=isDoubleSided) BOOL doubleSided; // Default is 'NO'.

这个属性定义的事书页的两面是否都显示内容,默认值为NO
如果为NO,则书页内容类似半透明的投射到背面。

当style为UIPageViewControllerTransitionStylePageCurl仿真翻页,并且UIPageViewControllerOptionSpineLocationKey值为UIPageViewControllerSpineLocationMid书脊在中间位置时,属性doubleSided 会被自动设置为YES,如果为NO,会崩溃。

三. 数据源与代理方法

pageViewController提供了类似UITableViewController的数据源与代理机制

1.UIPageViewControllerDataSource 数据源

iOS SDK提供了四个数据源方法,其中两个为@required,另外两个是@optional.

@required


- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;

这个方法是返回前一个页面的控制器,如果返回nil,则认为当前页面为第一个页面,不可以向前滚动或者翻页

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

个方法是返回后一个页面的控制器,如果返回nil,则认为当前页面为最后一个页面,不可以向后滚动或者翻页

@optional

这两个方法定义了在水平滚动模式下的一个pageControl,只有在滚动翻页UIPageViewControllerTransitionStyleScroll 并且水平滚动 UIPageViewControllerNavigationOrientationHorizontal时有效,会显示一个白色圆点的pageControl,在设置方法setViewControllers:...中后调用,会自动随着滚动更新指示器index

image.png
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController API_AVAILABLE(ios(6.0)); // The number of items reflected in the page indicator.

定义指示器的数量

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController API_AVAILABLE(ios(6.0)); // The selected item reflected in the page indicator.

定义指示器的当前index

2.UIPageViewControllerDelegate 代理

总共有5个可选代理

@optional

// Sent when a gesture-initiated transition begins.
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers API_AVAILABLE(ios(6.0));

这个方法是UIPageViewController开始滚动或翻页的时候触发

// Sent when a gesture-initiated transition ends. The 'finished' parameter indicates whether the animation finished, while the 'completed' parameter indicates whether the transition completed or bailed out (if the user let go early).
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed;

这个方法是在UIPageViewController结束滚动或翻页的时候触发

// Delegate may specify a different spine location for after the interface orientation change. Only sent for transition style 'UIPageViewControllerTransitionStylePageCurl'.
// Delegate may set new view controllers or update double-sided state within this method's implementation as well.
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation API_UNAVAILABLE(tvos);

这个方法是在style是 UIPageViewControllerTransitionStylePageCurl并且横竖屏状态变化的时候触发,我们可以重新设置书脊的位置,比如如果屏幕是竖屏状态的时候我们就设置书脊位置是UIPageViewControllerSpineLocationMin或UIPageViewControllerSpineLocationMax, 如果屏幕是横屏状态的时候我们可以设置书脊位置是UIPageViewControllerSpineLocationMid

- (UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController API_AVAILABLE(ios(7.0)) API_UNAVAILABLE(tvos);
- (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController API_AVAILABLE(ios(7.0)) API_UNAVAILABLE(tvos);

这两个方法设置了UIPageViewController支持的屏幕类型

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,440评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,814评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,427评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,710评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,625评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,014评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,511评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,162评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,311评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,262评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,278评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,989评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,583评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,664评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,904评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,274评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,856评论 2 339

推荐阅读更多精彩内容