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;
NSArray * PageObjectArray ;
UIButton * tempButton;
NSMutableArray * BtnObejctArray;
NSMutableArray * PageViewDataArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setPageViewControllerData];
[self addPageViewObject];
[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{
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];
[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;
NSInteger BtnTag = ClickButton.tag;
[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、對上面的對嗎進行總結:
4、如果要源碼的話,請留言,注明:郵箱;或者加我QQ 1542100658