并列控制器之间的切换

1、先看效果:


2、不做解释,应为解释都在代码里面。代码如下:

//

//  ViewController.m

//  并列页面间的切换

//

//  Created by MAC on 16/9/28.

//  Copyright © 2016 NetworkCode小贱. All rights reserved.

//


#import "ViewController.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "ThreeViewController.h"

#pragma mark - pageviewController 的两个代理 UIPageViewControllerDelegate and UIPageViewControllerDataSource

@interface ViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource>{

    UIPageViewController * MainPageViewController;

    /* 存放添加到PageViewController里面的对象*/

    NSArray * PageObjectArray ;

    /* 按钮的中间对象*/

    UIButton * tempButton;

    /* 按钮的数组*/

    NSMutableArray * BtnObejctArray;

    /* 要传入的数据数组*/

    NSMutableArray * PageViewDataArray;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    /* 页面加载数据的配置*/

    [self setPageViewControllerData];

    /* 创建要添加的数据*/

    [self addPageViewObject];

    /* 创建PageViewController的对象*/

    [self createPageViewObject];

    /* 创建导航*/

    [self CreateNavTitle];

    // Do any additional setup after loading the view, typically from a nib.

}

#pragma mark - 数据的设置

-(void)setPageViewControllerData{

    PageViewDataArray = [NSMutableArray arrayWithCapacity:0];

    NSMutableArray * ImageMutableArray = [NSMutableArray arrayWithCapacity:0];

    for (unsigned i =0; i<10; i++) {

        UIImage * Image = [UIImage imageNamed:[NSString stringWithFormat:@"Image-%d",i]];

        NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity:0];

        [dict setObject:Image forKey:@"image"];

        [dict setObject:[NSString stringWithFormat:@"wo de nice imageview -%d",i] forKey:@"title"];

        [dict setObject:@" 捕捉一只萤火,照亮一纸寂寞,纸上题满离愁,最押韵的竟是我的叹息。" forKey:@"content"];

        [ImageMutableArray addObject:dict];

        dict= nil;

    }

    [PageViewDataArray addObject:ImageMutableArray];

    ImageMutableArray = nil;

    [PageViewDataArray addObject:@"https://www.baidu.com"];

    NSArray * ThreeDataArray = @[@" 爱情的最初",@" 所要的爱情所要的幸福,只是一天天平淡日子里的执手相看,只是一天天时光流逝中的快乐相随,只是一天天简单地问候,只是相处时温暖的关怀,只是有那么一天,我们走进婚姻时的美丽瞬间。"];

    [PageViewDataArray addObject:ThreeDataArray];

    ThreeDataArray = nil;

}

#pragma mark - addPageViewObject

-(void)addPageViewObject{

    FirstViewController * FirstPage = [[FirstViewController alloc]init];

    SecondViewController * SecondPage =[[SecondViewController alloc]init];

    ThreeViewController * ThreePage = [[ThreeViewController alloc]init];

    PageObjectArray = @[FirstPage,SecondPage,ThreePage];

}

#pragma mark 创建PageViewController的对象

-(void)createPageViewObject{

    /* PageViewController 进行配置*/

    NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:PageObjectArray.count] forKey:UIPageViewControllerOptionInterPageSpacingKey];

    /* 创建对象*/

    MainPageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];

    /* 设置代理*/

    MainPageViewController.delegate = self;

    MainPageViewController.dataSource = self;

    /* 设置大小*/

    MainPageViewController.view.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64);

    /* 进行渲染*/

    [self addChildViewController:MainPageViewController];

    [self.view addSubview:MainPageViewController.view];

}

#pragma mark PageViewController 里面添加对象 注意:这里是按钮点击的控制器的数据传入

-(void)addPageViewControllerObject:(NSInteger)index{

    /* 创建一个间接对象*/

    UIViewController * TempViewController = nil;

    if (0==index) {

        FirstViewController * first = PageObjectArray[index];

        first.ListDataArray = PageViewDataArray[0];

        TempViewController = first;

    }else if (1==index){

        SecondViewController * second = PageObjectArray[index];

        second.LoadUrlString = PageViewDataArray[1];

        TempViewController = second;

    }

    else if(2==index){

        ThreeViewController * Three =  PageObjectArray[index];

        Three.ThreeDataArray = PageViewDataArray[2];

        TempViewController = Three;

    }

    NSArray * viewControllers = @[TempViewController];

    /* 这里设置PageViewController 显示那个控制器 ,也是按钮点击PageViewController显示的控制设置方法*/

    [MainPageViewController setViewControllers:viewControllers

                                  direction:UIPageViewControllerNavigationDirectionForward

                                   animated:NO

                                 completion:nil];

}

#pragma mark - 根据index得到对应的UIViewController


#pragma mark PageViewController的代理事件

/* 页面间的切换会调用*/

-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers{

    NSInteger Index = [PageObjectArray indexOfObject:[pendingViewControllers firstObject]];

    [self btnChange:Index];

}

/* 页面之间跳转完成后的调用*/

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{

    

}

#pragma mark - 下面两个是pageView数据的传递方法

-(UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{

    NSInteger index = [PageObjectArray indexOfObject:viewController];

    UIViewController * TempViewController = [UIViewController new];

    if (index==PageObjectArray.count-1) {

        return nil;

    }

    index++;

    if (0==index) {

        FirstViewController * first = PageObjectArray[index];

        first.ListDataArray = PageViewDataArray[0];

        TempViewController = first;

    }else if (1==index){

        SecondViewController * second = PageObjectArray[index];

        second.LoadUrlString = PageViewDataArray[1];

        TempViewController = second;

    }

    else if(2==index){

        ThreeViewController * Three =  PageObjectArray[index];

        Three.ThreeDataArray = PageViewDataArray[2];

        TempViewController = Three;

    }

    return TempViewController;

}

-(UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{

    NSInteger index = [PageObjectArray indexOfObject:viewController];

    if (index<=0) {

        return nil;

    }

    index--;

    return PageObjectArray[index];

}

-(void)CreateNavTitle{

    NSInteger PageViewObjectCount = 3;

    UIView * NavView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

    [self.view addSubview:NavView];

    UIScrollView * ScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, NavView.bounds.size.width, 44)];

    /* 添加按钮*/

    NSArray * ButtonTitle = @[@"列表",@"网页",@"自定义"];

    BtnObejctArray = [NSMutableArray arrayWithCapacity:0];

    for (unsigned int i = 0; i<PageViewObjectCount; i++) {

        UIButton * Button = [UIButton buttonWithType:UIButtonTypeCustom];

        Button.frame = CGRectMake(i * ((NavView.bounds.size.width - PageViewObjectCount +1)/PageViewObjectCount+1), 0, (NavView.bounds.size.width - PageViewObjectCount +1)/PageViewObjectCount, 44);

        [Button setTitle:ButtonTitle[i] forState:UIControlStateNormal];

        [Button setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];

        [Button setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];

        Button.tag = i;

        [Button addTarget:self action:@selector(ButtonClick:) forControlEvents:UIControlEventTouchUpInside];

        Button.layer.masksToBounds = YES;

        Button.layer.borderWidth = 0.5;

        Button.layer.borderColor = [UIColor cyanColor].CGColor;

        if (0==i) {

            [self ButtonClick:Button];

        }

        [BtnObejctArray addObject:Button];

        [ScrollView addSubview:Button];

    }

    [NavView addSubview:ScrollView];

}

#pragma mark - 按钮的点击事件

-(void)ButtonClick:(UIButton*)ClickButton{

    tempButton.selected =NO;

    tempButton = ClickButton;

    tempButton.selected = YES;

    /* 获取按钮的tag*/

    NSInteger BtnTag = ClickButton.tag;

    /* PageViewController 里面添加对象*/

    [self addPageViewControllerObject:BtnTag];

}

#pragma mark - 按钮的色变

-(void)btnChange:(NSInteger)index{

    tempButton.selected =NO;

    tempButton = BtnObejctArray[index];

    tempButton.selected = YES;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end


3、对上面的对吗进行总结:

/****************************UIPageViewController 的使用总结*******************************/

/*

   1 创建一个UIPageViewController 的对象。

       - (instancetype)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(nullable NSDictionary<NSString *, id> *)options NS_DESIGNATED_INITIALIZER;

       其中:

       UIPageViewControllerTransitionStyle  的样式有:

            1 翻书的效果

            UIPageViewControllerTransitionStylePageCurl = 0, // Navigate between views via a page curl transition.   

            2Scroollview 一样的滑动

            UIPageViewControllerTransitionStyleScroll = 1 // Navigate between views by scrolling.

       

       UIPageViewControllerNavigationOrientation PageViewController 的滑动方向有:

 

            1》水平

            UIPageViewControllerNavigationOrientationHorizontal = 0,

            2》垂直

            UIPageViewControllerNavigationOrientationVertical = 1


       options 对于PageViewController 的设置:

            NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:PageObjectArray.count] forKey:UIPageViewControllerOptionInterPageSpacingKey];

       其中

            1 Value

            Option Value 是一个数,使用 Nsnumber 包装的也可以 “@4这样写。

            2 key 

                ***

                1》》 用于像翻书效果

                UIKIT_EXTERN NSString * const UIPageViewControllerOptionSpineLocationKey;

                2》》 用于ScrollView 样的滑动

                UIKIT_EXTERN NSString * const UIPageViewControllerOptionInterPageSpacingKey NS_AVAILABLE_IOS(6_0);

                ***

    2 设置代理

        <UIPageViewControllerDelegate,UIPageViewControllerDataSource>

    3 设置PageViewController大小 frame

        A.view.frame = CGRectMake(x,y,w,h); 不要错误哈

    4 创建要添加到PageViewController里面的对象,例如:

        FirstViewController * FirstPage = [[FirstViewController alloc]init];

        SecondViewController * SecondPage =[[SecondViewController alloc]init];

        ThreeViewController * ThreePage = [[ThreeViewController alloc]init];

        PageObjectArray = @[FirstPage,SecondPage,ThreePage];

    5 设置PageViewController 首先要显示的哪一个控制器 ,方法是:

        - (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;

        其中:

            @ viewControllers  参数,是要传入的一个对象,就是你想要在PageViewController 里面第一个要显示的

            @ UIPageViewControllerNavigationDirection 改参数是控制控制器之间切换的方向,有两个可选的参数:

                1 向前

                UIPageViewControllerNavigationDirectionForward,

                2 向后

                UIPageViewControllerNavigationDirectionReverse


    6 PageViewController 添加到主控制器,方法是:

        - (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0);

    7 PageViewController View渲染到主控制器上:例如:

        [self.view addSubview:MainPageViewController.view];

    8 实现PageViewController 的几个协议

 */


4、如果要源码的话,请留言,注明:邮箱;或者加我QQ 1542100658 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值