UIPageViewController
介绍:
属性介绍
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationOrientation) {
UIPageViewControllerNavigationOrientationHorizontal = 0,// 水平方向翻页过渡
UIPageViewControllerNavigationOrientationVertical = 1// 垂直方向翻页过渡
};
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'.
typedef NS_ENUM(NSInteger, UIPageViewControllerTransitionStyle) {
UIPageViewControllerTransitionStylePageCurl = 0, // 纸质翻页效果过渡
UIPageViewControllerTransitionStyleScroll = 1 // 平滑滚动翻页效果过渡
};
// 设置pageViewController 首页array顺序
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationDirection) {
UIPageViewControllerNavigationDirectionForward,// 正序(左到右、上到下)
UIPageViewControllerNavigationDirectionReverse//逆序 (右到左、下到上)
};
@property (nonatomic, getter=isDoubleSided) BOOL doubleSided;
// 一次翻页的两页。默认为No,一次为一页,Yes为翻两页
//除了用 UIPageViewControllerSpineLocationMid 时为Yes,其余的时候为No
@property (nullable, nonatomic, readonly) NSArray<__kindof UIViewController *> *viewControllers; // 通过setViewControllers:方法将View
UIPageViewControllerDelegate
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers NS_AVAILABLE_IOS(6_0) // 将要过渡要目标ViewController时调用
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed;//过渡动画结束,
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation ;
// 根据屏幕旋转方向设置书脊(spine Location)位置和初始化首页、重新设置doubleSided
UIPageViewControllerDataSource
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
// 往前翻页
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
// 往后翻页
使用方法
创建两个ViewController:ReaderViewController 和 ReaderPagesViewController
ReaderViewController
1、首先声明两个属性
@property (nonatomic, strong)UIPageViewController *pageViewController;// 页面试图控制器
@property (nonatomic, strong)ReaderPagesViewController *readerPagesViewController; //当前阅读页面 展示控制器
2、Layz Init
- (UIPageViewController *)pageViewController{
if (!_pageViewController) {
_pageViewController = [[UIPageViewController alloc]
initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
_pageViewController.delegate = self;
_pageViewController.dataSource = self;
[self.view addSubview:_pageViewController.view];
}
return _pageViewController;
}
- (ReaderPagesViewController *)readerPagesViewController{
if (!_readerPagesViewController) {
_readerPagesViewController = [[ReaderPagesViewController alloc] init];
}
return _readerPagesViewController;
}
3、设置pageViewController的 ViewControllerArray和设置 childViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addChildViewController:self.pageViewController];
[_pageViewController setViewControllers:@[[[ReaderPagesViewController alloc] init]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
4、实现UIPageViewControllerDelegate 和 UIPageViewControllerDataSource代理方法
#pragma mark - UIPageViewControllerDelegate
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
// 用来获取 当前阅读页面的page&Chapter,目前用不到
}
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{
// 用来处理 当前页面的page&Chapter累加用的 目前用不到
}
#pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
// 前进翻页都 生成一个新控制器
return [[ReaderPagesViewController alloc] init];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
// 后退翻页都 生成一个新控制器
return [[ReaderPagesViewController alloc] init];
}
ReaderPagesViewController
只需要设置个背景色就好了,方便看效果
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor =[UIColor whiteColor];
}