PageViewController

先看下效果。


2016-12-10 15_01_16.gif

代码如下可以看下

//
//  ViewController.m
//  PageVCDomo
//
//  Created by mibo02 on 16/12/10.
//  Copyright © 2016年 mibo02. All rights reserved.
//

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

@end

@implementation ViewController

- (NSArray *)pageContentArray
{
    if (!_pageContentArray) {
        NSMutableArray *arrayM = [[NSMutableArray alloc] init];
        for (int i = 1 ; i < 10; i++) {
            NSString *contentString = [[NSString alloc] initWithFormat:@"第%d页",i];
            [arrayM addObject:contentString];
        }
        _pageContentArray = [[NSArray alloc] initWithArray:arrayM];
    }
    return _pageContentArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //设置page的配置项
    NSDictionary *dic = @{UIPageViewControllerOptionInterPageSpacingKey :@(10)};
    //UIPageViewControllerTransitionStylePageCurl<页码翻滚>
    //UIPageViewControllerTransitionStyleScroll<滑动翻滚>
    _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:1 navigationOrientation:(UIPageViewControllerNavigationOrientationHorizontal) options:dic];
    //
    _pageViewController.delegate =self;
    _pageViewController.dataSource = self;
    ContentViewController *initVC = [self viewControllerAtIndex:0];//得到第一页
    NSArray *viewCongrollers = [NSArray arrayWithObject:initVC];
    [_pageViewController setViewControllers:viewCongrollers direction:(UIPageViewControllerNavigationDirectionReverse) animated:NO completion:nil];
    _pageViewController.view.frame = self.view.bounds;
    //
    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [self indexOfViewController:(ContentViewController *)viewController];
    if (index == 0 || index == NSNotFound) {
        return nil;
    }
    index--;
    return [self viewControllerAtIndex:index];
}

//数组元素值,得到下标值
- (NSUInteger)indexOfViewController:(ContentViewController *)viewController
{
    return [self.pageContentArray indexOfObject:viewController.content];
}
#pragma mark 返回下一个ViewController对象

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    
    NSUInteger index = [self indexOfViewController:(ContentViewController *)viewController];
    if (index == NSNotFound) {
        return nil;
    }
    index++;
    if (index == [self.pageContentArray count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
    
    
}

#pragma mark --根据index得到对应的VC
- (ContentViewController *)viewControllerAtIndex:(NSUInteger )index
{
    if (self.pageContentArray.count == 0 || index >= self.pageContentArray.count) {
        return nil;
    }
    //创建一个新的控制器,并分配相应的数据
    ContentViewController *contentVC = [[ContentViewController alloc] init];
    contentVC.content = self.pageContentArray[index];
    return contentVC;
}

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


@end

//
//  ContentViewController.h
//  UIPageViewControllerDemo
//
//  Created by Herb on 16/4/12.
//  Copyright © 2016年 Herb. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ContentViewController : UIViewController

@property (nonatomic, copy) NSString *content;

@end
//
//  ContentViewController.m
//  UIPageViewControllerDemo
//
//  Created by Herb on 16/4/12.
//  Copyright © 2016年 Herb. All rights reserved.
//

#import "ContentViewController.h"
#define kRandomColor ([UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0f])

@interface ContentViewController ()

@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation ContentViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = kRandomColor;
    _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 50, self.view.bounds.size.width, 50)];
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont systemFontOfSize:20];
    _contentLabel.textAlignment = NSTextAlignmentCenter;
    _contentLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_contentLabel];
}

- (void) viewWillAppear:(BOOL)paramAnimated{
    [super viewWillAppear:paramAnimated];
    _contentLabel.text = _content;
//    [self.myWebView loadHTMLString:_dataObject baseURL:nil];
//    [self.view addSubview:self.myWebView];
    
}

@end

也可以看下这位大神写的。
http://www.jianshu.com/p/46c8c585d50b
//demo在这里
https://github.com/fengfengaima/PageViewController.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,901评论 25 709
  • ——丁酉年,春 注定了这个春天的不一般,一次与篆书、篆刻的特别结缘,一个精神饱和、内心宁静、没空说话的月圆之夜,还...
    敏姑娘33阅读 4,294评论 2 3
  • 朋友朱力是开餐馆的。 而且是一直掌勺颠锅的。 得到他的邀请不是很容易,只是因为他太忙。昨天,他突然来电话说他买到了...
    竖秋阅读 3,940评论 2 0
  • 文本编辑器:EditPlus 文本编辑器,常用于文本编辑、转码、搜索、Hex查看。尤其是文本搜索,可以查询所有子文...
    犀利兔子哥阅读 3,607评论 0 0
  • 文/左毅 天涯海角最相知 一直以来,文字,于我而言,不过是一种记录,一种自己与自己对话的方式。最喜在安静的深夜用文...
    左毅阅读 4,829评论 1 8

友情链接更多精彩内容