[关闭]
@qidiandasheng 2017-03-01T14:50:16.000000Z 字数 4476 阅读 2780

UIPageViewController使用

iOS界面开发


简介

UIPageViewController作为子控制器加载在viewController上。作为容器可以很简单的实现图片轮播和翻书效果。

使用

初始化

  1. - (instancetype)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style
  2. navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation
  3. options:(nullable NSDictionary<NSString *, id> *)options NS_DESIGNATED_INITIALIZER;

上面初始化方法有3个参数:

初始化完成后直接加入到当前UIViewControllerChildViewController:

  1. [self addChildViewController:_pageViewController];
  2. [self.view addSubview:_pageViewController.view];

doubleSided

  1. @property (nonatomic, getter=isDoubleSided) BOOL doubleSided; // Default is 'NO'.

这个值默认为NO,表示初始页面是否是两面的。只有在style为UIPageViewControllerTransitionStylePageCurl,options为@{UIPageViewControllerOptionSpineLocationKey:@(UIPageViewControllerSpineLocationMin)}时必须设置为YES。

设置初始页面

  1. - (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers
  2. direction:(UIPageViewControllerNavigationDirection)direction
  3. animated:(BOOL)animated
  4. completion:(void (^ __nullable)(BOOL finished))completion;

UIPageViewController是一个容器,那么就需要设置它的初始页面。

数据源和代理

数据源

@required

  1. //返回上一个ViewController对象
  2. - (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
  3. //返回下一个ViewController对象
  4. - (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

@optional

下面这两个是可选的。用于styleUIPageViewControllerTransitionStyleScroll的情况,主要用于显示轮播下面的小圆点。

  1. //一共多少个小圆点
  2. - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0); // The number of items reflected in the page indicator.
  3. //初始时选中的小圆点在第几个位置
  4. - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0); // The selected item reflected in the page indicator.

代理

代理都是可选的。

@optional

  1. //将要翻页或滚动时触发
  2. - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers NS_AVAILABLE_IOS(6_0);
  3. //结束滚动或翻页的时候触发
  4. - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed;

参考

UIPageViewController教程
懒加载的PageViewController

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注