IOS 视图跳转/层级相关的知识IOS 知识积累收藏

[iOS]UIPageViewController使用--API

2017-02-24  本文已影响1626人  流火绯瞳

关于** UIPageViewController**, 在之前做翻书效果的时候使用过一次(iOS UIPageViewController - 使用总结), 之后就再没怎么接触, 后来发现这个类值得学习的地方, 还是很多的, 在这里再重新学习一下这个类.

打开底层的API, 会发现其接口十分简单, 主要是其代理方法和一个初始化方法, 其他的很多属性都是只读的 , 但并不是就不需要设置, 这些都放在了其初始化方法里了.

初始化

系统提供的初始化方法主要是下面这个:

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

简单介绍下这里的几个参数:

  • style : 控制器的类型, 枚举, 有以下两种
typedef NS_ENUM(NSInteger, UIPageViewControllerTransitionStyle) {
// 上下翻页, 类似翻书
    UIPageViewControllerTransitionStylePageCurl = 0, // Navigate between views via a page curl transition.
// 左右滑动 
    UIPageViewControllerTransitionStyleScroll = 1 // Navigate between views by scrolling. 
};
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationOrientation) {
// 水平方向(左右滑动或翻页)
    UIPageViewControllerNavigationOrientationHorizontal = 0,
// 竖直方向(上下滑动或翻页)
    UIPageViewControllerNavigationOrientationVertical = 1
};
仅在style为UIPageViewControllerTransitionStylePageCurl时有效;
对应的值为NSNumber对象, 包含的数值也是一个枚举:
typedef NS_ENUM(NSInteger, UIPageViewControllerSpineLocation) {
    UIPageViewControllerSpineLocationNone = 0, // Returned if 'spineLocation' is queried when 'transitionStyle' is not 'UIPageViewControllerTransitionStylePageCurl'.
// 单页显示, 从上往下翻页
    UIPageViewControllerSpineLocationMin = 1,  // Requires one view controller.
// 双页显示
    UIPageViewControllerSpineLocationMid = 2,  // Requires two view controllers.
// 单页显示, 从下往上翻
    UIPageViewControllerSpineLocationMax = 3   // Requires one view controller.
};   // Only pertains to 'UIPageViewControllerTransitionStylePageCurl'.

使用举例: 设置为双页显示

NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid] forKey:UIPageViewControllerOptionSpineLocationKey];
NSDictionary *option = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:10.0] forKey:UIPageViewControllerOptionInterPageSpacingKey];
属性

这里仅介绍可设置的属性, 大部分的属性都是只读的

实例方法

这个方法主要是设置某个控制器可视化, 也就是显示某个控制器

- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion

参数:

  • viewControllers: 待显示的控制器
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationDirection) {
// 正向切换
    UIPageViewControllerNavigationDirectionForward,
// 反向切换
    UIPageViewControllerNavigationDirectionReverse
};

以上便是UIPageViewController的初始化, 一些可配置属性以及实例方法, 下面就是其代理方法和数据源方法了;

UIPageViewControllerDataSource 数据源方法

主要使用到的, 而且是必须实现的数据源方法是以下两个, 用于返回下一个控制器

// 返回当前控制器的前一个控制器
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
// 返回当前控制器的后一个控制器
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

还有两个可选实现的数据源方法:

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0); // The number of items reflected in the page indicator.
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController 

这两个方法使用, 可以参考这个回答stackoverflow

UIPageViewControllerDelegate 代理方法
// 手势滑动(或翻页)开始时回调
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers 
// 滑动结束时的回调
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed

上面第二个方法的参数中的两个BOOL值简单说明一下:

  • finished : 滑动(或者翻页)动画是否完成

在我们进行滑动(或翻页)的时候, 如果滑动(或翻页)的幅度不够, 控制器会恢复至原有的控制器状态, 不会切换到下一个, 这两个BOOL值就是用来标识动画的状态, 和是否切换成功的状态;

// 根据orientation切换UIPageViewControllerSpineLocation
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation __TVOS_PROHIBITED;
// 返回支持的方法
- (UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
// 优先使用的方向
- (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController 

下一篇文章, [iOS]UIPageViewController使用--实战

(完)

上一篇下一篇

猜你喜欢

热点阅读