1.首先将UIPageViewController的切换方式设置为翻书样式,并实例化UIPageViewController对象,根据给定的属性
NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]forKey: UIPageViewControllerOptionSpineLocationKey];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options: options];
2.设置双面显示
_pageController.doubleSided = YES;
3.将将要显示的前两个控制器先赋值给pageViewController,此处为双面,所以赋值的时候一定要有两个控制器
MoreViewController *initialViewController =[self viewControllerAtIndex:0];// 得到第一页
MoreViewController *initialViewController2 =[self viewControllerAtIndex:1];// 得到第二页
NSArray *viewControllers =[NSArray arrayWithObjects:initialViewController,initialViewController2, nil];
[_pageController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
3.设置代理并实现代理方法
_pageController.dataSource = self;
#pragma mark- UIPageViewControllerDataSource
// 返回上一个ViewController对象
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
NSUInteger index = [self indexOfViewController:(MoreViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
MoreViewController * vc = [self viewControllerAtIndex:index];
return vc;
}
// 返回下一个ViewController对象
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
NSUInteger index = [self indexOfViewController:(MoreViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageContent count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
数据源方法中用到的自定义方法
// 得到相应的VC对象
- (MoreViewController *)viewControllerAtIndex:(NSUInteger)index {
if (([self.pageContent count] == 0) || (index >= [self.pageContent count])) {
return nil;
}
// 创建一个新的控制器类,并且分配给相应的数据
MoreViewController *dataViewController =[[MoreViewController alloc] init];
dataViewController.dataObject =[self.pageContent objectAtIndex:index];
return dataViewController;
}
// 根据数组元素值,得到下标值
- (NSUInteger)indexOfViewController:(MoreViewController *)viewController {
return [self.pageContent indexOfObject:viewController.dataObject];
}
经过观察,执行过程大致是这样的,首先出现的是 第0页,第1页,当我们往后翻的时候,程序需要加载出来后面两页的内容,于是,就执行了两次第二个数据源方法,得到后面两个页面以后,就可以进一步的显示了,同理,往前翻也是一样的。
4. 定义“这本书”的尺寸,并添加到当前视图和控制器上
[[_pageController view] setFrame:CGRectMake(0,0.5*([UIScreen mainScreen].bounds.size.height-300), [UIScreen mainScreen].bounds.size.width,300)];
[self addChildViewController:_pageController];
[[self view] addSubview:[_pageController view]];// 设置UIPageViewController的配置项
5.貌似是写完了,但是显示出来跟预想的差距很大,往后翻的时候是正常的,当时往前翻页的时候左侧的视图总是遮盖右侧的视图,额,在查阅了当前的view的所有分布层次以后,才发现,原来是我自定义的控件设置的尺寸没有随着双面而缩小,于是把右侧的也挡住了,所以,你可以设置自定义的控制器的view.clipToBounds ,最后再调整一下view上面控件的大小
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
self.label.frame = self.view.bounds;
}
6.可以看结果了~
网友评论