今天再看一个旧项目时,发现这里面用的了UIPageViewController这个控件(可以理解为书籍控件)。之前只是听说过,从来没有去实际运用过。所以就来看看UIPageViewController的基本使用。本文只是对UIPageViewController它的基本介绍,对于大牛级别的可以绕行。
UIPageViewController也有自己的delegate和dataSource 相信这两个熟悉在做iOS开发使用UITableView你已经很熟悉了。dataSource 也有两个必须实现的协议方法,分别用了返回当前控制器的上个控制器和当前控制器的下一个控制器。
在使用UIPageViewController控件时尤为注意的时当使用UIPageViewControllerOptionSpineLocationKey的值为UIPageViewControllerSpineLocationMid时也就书脊的位置在中间,同时显示两页内容初始设置显示时一定要设置两个自控制器 否则会报错。如果只显示一页内容则初始设置时只是一个一个控制器。
实例代码如下:
import "ViewController.h"
@interface ViewController ()<UIPageViewControllerDelegate, UIPageViewControllerDataSource>
@property (nonatomic, strong)UIPageViewController *pageVc;
@property (nonatomic, strong)NSMutableArray *viewControllers;
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];
/* style :这个参数是UIPageViewController翻页的过渡样式,系统提供了两种过度样式,分别是- UIPageViewControllerTransitionStylePageCurl : 卷曲样式类似翻书效果
- UIPageViewControllerTransitionStyleScroll : UIScrollView滚动效果
- navigationOrientation : 这个参数是UIPageViewController导航方向,系统提供了两种方式,分别是
- UIPageViewControllerNavigationOrientationHorizontal : 水平导航方式
- UIPageViewControllerNavigationOrientationVertical : 垂直导航方式
- options : 这个参数是可选的,传入的是对UIPageViewController的一些配置组成的字典,不过这个参数只能以UIPageViewControllerOptionSpineLocationKey和UIPageViewControllerOptionInterPageSpacingKey这两个key组成的字典.
- UIPageViewControllerOptionSpineLocationKey 这个key只有在style是翻书效果UIPageViewControllerTransitionStylePageCurl的时候才有作用, 它定义的是书脊的位置,值对应着UIPageViewControllerSpineLocation这个枚举项,不要定义错了哦.
- UIPageViewControllerOptionInterPageSpacingKey这个key只有在style是UIScrollView滚动效果UIPageViewControllerTransitionStyleScroll的时候才有作用, 它定义的是两个页面之间的间距(默认间距是0).
*/
// NSDictionary options = @{UIPageViewControllerOptionInterPageSpacingKey : @(20)};
/
UIPageViewControllerSpineLocationNone = 0, // 默认UIPageViewControllerSpineLocationMin
UIPageViewControllerSpineLocationMin = 1, // 书棱在左边
UIPageViewControllerSpineLocationMid = 2, // 书棱在中间,同时显示两页
UIPageViewControllerSpineLocationMax = 3 // 书棱在右边
*/
NSDictionary *options = @{UIPageViewControllerOptionSpineLocationKey : [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]};
self.pageVc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];self.pageVc.delegate = self;
self.pageVc.dataSource = self;self.pageVc.view.frame = self.view.bounds;
// 要显示的第几页
// NSArray *vcs = [NSArray arrayWithObject:self.viewControllers[2]];// 如果要同时显示两页,options参数要设置为UIPageViewControllerSpineLocationMid
NSArray *vcArray = [NSArray arrayWithObject:self.viewControllers[0]];[self.pageVc setViewControllers:vcArray direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
// 保存控制器
[self addChildViewController:self.pageVc];
// 显示控制器
[self.view addSubview:self.pageVc.view];
}
// 根据数组元素,得到下标值
- (NSUInteger)indexOfViewController:(UIViewController *)viewControlller {
return [self.viewControllers indexOfObject:viewControlller];
}
pragma mark - UIPageViewControllerDataSource
/*
- 参数ViewController为当前正在显示的VC控制器
return 的ViewController为将要显示的VC控制器
*/
// 返回上一个ViewController对象
-
(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [self indexOfViewController:viewController];if (index == 0 || index == NSNotFound) {
return nil;
}
index--;return self.viewControllers[index];
}
// 返回下一个ViewController对象 -
(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [self indexOfViewController:viewController];
if (index == NSNotFound) {
return nil;
}
index++;if (index == [self.viewControllers count]) {
return nil;
}return self.viewControllers[index];
}
pragma mark - UIPageViewControllerDelegate
// 开始翻页调用
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers NS_AVAILABLE_IOS(6_0) {
NSLog(@"1");
}
// 翻页完成调用
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
NSLog(@"2");
}
//这个方法是在style是UIPageViewControllerTransitionStylePageCurl 并且横竖屏状态变化的时候触发,我们可以重新设置书脊的位置,比如如果屏幕是竖屏状态的时候我们就设置书脊位置是UIPageViewControllerSpineLocationMin或UIPageViewControllerSpineLocationMax, 如果屏幕是横屏状态的时候我们可以设置书脊位置是UIPageViewControllerSpineLocationMid
-(UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation{
return UIPageViewControllerSpineLocationMin;
}
pragma mark -
-
(NSMutableArray *)viewControllers {
if (!_viewControllers) {
_viewControllers = [NSMutableArray array];
for (int i = 1; i <= 10; i++) {
UIViewController *VC = [UIViewController new];UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 30, 30)]; label.text = [NSString stringWithFormat:@"%d",i]; [VC.view addSubview:label]; VC.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]; [_viewControllers addObject:VC]; }
}
return _viewControllers;
}
@end