UIPageViewController 的简单使用

这几天一直在研究一个类似书本翻页的效果,(即书脊(spine)在中间的效果)

这个效果我找了好几天,什么openGL,Layer,pageCurl效果都看过了,其中code4app,github上有许多类似的效果,都感觉不错,始终觉得UIPageViewController原生的类应该会有类似的解决办法,不容易啊!

好吧,废话不多说,直接上代码

//
//  ViewController.m
//  UIPageVIewControllerDemo
//
//  Created by HaiBing on 15/7/26.
//  Copyright (c) 2015年 Nfschina. All rights reserved.
//

#import "ViewController.h"
#import "ContentViewController.h"
@interface ViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
@property (nonatomic,strong)UIPageViewController *pageController;
@property (nonatomic,strong)NSArray *pageContent;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createContentPages];
//  UIPageViewControllerSpineLocationMid这个参数设置书脊的位置,这里设置在中间
    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMid] forKey:UIPageViewControllerOptionSpineLocationKey];
    
    self.pageController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
    self.pageController.dataSource = self;
    CGRect rect = self.view.bounds;
//    rect.size.width /= 2;
//    rect.origin.x = rect.size.width;
    [self.pageController.view setFrame:rect];
    //既然书脊在中间,那肯定有两页,默认第一页和第二页
     ContentViewController *firstViewController = [self viewControllerAtIndex:0];
    ContentViewController *secondViewController = [self viewControllerAtIndex:1];
    NSArray *viewControllers = [NSArray arrayWithObjects:firstViewController,secondViewController, nil];
    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    [self addChildViewController:self.pageController];
    [self.view addSubview:self.pageController.view];
    
    
}
- (ContentViewController *)viewControllerAtIndex:(NSInteger)index
{
    if (self.pageContent.count == 0 || index >= self.pageContent.count) {
        return  nil;
    }
    ContentViewController *dataViewController = [[ContentViewController alloc]init];
    dataViewController.dataObject = [self.pageContent objectAtIndex:index];
    return dataViewController;
}
- (NSInteger)indexOfViewController:(MoreViewController *)viewController
{
    return  [self.pageContent indexOfObject:viewController.dataObject];
}
//初始化数据,像tableView一样
- (void)createContentPages
{
    NSMutableArray *pagesStrings = [[NSMutableArray alloc]init];
    for (int i = 0; i < 10; i++) {
        
        NSString *contentString = [NSString stringWithFormat:@"00%d.jpg",i];
        [pagesStrings addObject:contentString];
    }
    self.pageContent = [pagesStrings copy];
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    if (completed) {
     
        ContentViewController *previousViewController =  (ContentViewController*)previousViewControllers.lastObject;
        previousViewController.view.hidden = true;
    }
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = [self indexOfViewController:(ContentViewController *)viewController];
    if (index == 0 || index == NSNotFound) {
        return nil;
    }
    index--;
    return  [self viewControllerAtIndex:index];
    
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSInteger index = [self indexOfViewController:(ContentViewController *)viewController];
    if (index == NSNotFound) {
        return  nil;
    }
    index++;
    if (index == self.pageContent.count) {
        return  nil;
    }
    return [self viewControllerAtIndex:index];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

然后再ContentViewController中

@interface ContentViewController : UIViewController
@property (nonatomic,strong)id dataObject;
@end


//  ContentVIewCOntroller.m
//  UIPageVIewControllerDemo
//
//  Created by HaiBing on 15/7/26.
//  Copyright (c) 2015年 Nfschina. All rights reserved.
//

#import "<span style="font-family: Arial, Helvetica, sans-serif;">ContentVIewCOntroller</span>.h"

@interface MoreViewController ()

@end

@implementation <span style="font-family: Arial, Helvetica, sans-serif;">ContentVIewCOntroller</span>


- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *imageName = (NSString *)_dataObject;
    CGRect rect = self.view.bounds;
    rect.size.width /= 2;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:rect];
    imageView.image = [UIImage imageNamed:imageName];
    [self.view addSubview:imageView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 
 实现书本那样翻页的效果就这么简单,想要实现单页的就只需把 
UIPageViewControllerSpineLocationMid改成 
UIPageViewControllerSpineLocationMin,然后把 
NSArray *viewControllers = [NSArray arrayWithObjects:firstViewController, nil];改成这样即可。是不是比code4app上德哪些简单。 

好了,运行一下,测一测吧。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值