PageViewController

作者: 我想走走 | 来源:发表于2016-12-10 15:05 被阅读130次

    先看下效果。


    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

    相关文章

      网友评论

        本文标题:PageViewController

        本文链接:https://www.haomeiwen.com/subject/ajqumttx.html