UIPageViewController的使用及一些坑

UIPageViewController是继承UIViewController,分页控制器,但不能单独创建使用。

1 创建方法

_pageViewController= [[UIPageViewControlleralloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScrollnavigationOrientation:UIPageViewControllerNavigationOrientationHorizontaloptions:@{UIPageViewControllerOptionSpineLocationKey:@0,UIPageViewControllerOptionInterPageSpacingKey:@5}];

里面的style,orientation,LocationKey不多说了,查看官方文档即可。

:创建该方法需要新建ViewController,再新建ViewController中初始化UIPageViewController,最后addSubView,addChildView方式加载UIPageViewController。

UIPageViewController加载显示Controller,用以下方法:

 - (void)setViewControllers:(nullableNSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void(^__nullable)(BOOLfinished))completion;

:viewControllers只加载显示controllers,切勿将所有的controllers存进去。一般将首个controller以数组封装存入。


2 使用

主要是UIPageViewController几个方法

向前翻

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

向后翻

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

注:这两个方法在缓慢拖拽时不触发,需要用到下两个方法

a 将要偏移方法,这里面pendingViewControllers是进行时Controller数组

- (void)pageViewController:(UIPageViewController*)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllersNS_AVAILABLE_IOS(6_0);

b 结束动画方法,一定要根据completed判断是否结束,YES表示偏移完成,NO表示取消,如此判定当前的index, 注意不要使用previousViewControllers判断index,需要根据上面的方法来判定

- (void)pageViewController:(UIPageViewController*)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed;


取消pageControl,想要取消白点显示,只要不设定以下dataSource方法即可。

- (NSInteger)presentationCountForPageViewController:(UIPageViewController*)pageViewController

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController*)pageViewController

你可能感兴趣的:(UIPageViewController的使用及一些坑)