博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIPageViewController-浅析
阅读量:6859 次
发布时间:2019-06-26

本文共 6655 字,大约阅读时间需要 22 分钟。

http://blog.sina.com.cn/s/blog_7b9d64af0101c2zg.html
 
一、
UIPageViewController
概念
 
控件为我们提供了一种像翻书效果的一种控件。我们可以通过使用UIPageViewController控件,来完成类似图书一样的翻页控制方式。
 
二、使用
UIPageViewController
控件
 
先假象一下,一本书大概可以分为:1.每一页。2.每一页中相应的数据。
使用UIPageViewController控件,也是类似的两个构成部分。要有一个书的框架,来控制页;每一页的内容。
 
1.创建一个ViewController,包含一个UIPageViewController来控制显示,一个NSArray包括所有数据。
 
定义这个ViewController类,并使用
UIPageViewController来管理每一页,并提供数据。
 
PageAppViewController.h 
 

@interface PageAppViewController :UIViewController<</span>UIPageViewControllerDataSource>{

 

}

 

@property (strong, nonatomic) UIPageViewController *pageController;

@property (strong, nonatomic) NSArray *pageContent;

 

@end

 
PageAppViewController.m
 

#import "PageAppViewController.h"

#import "MoreViewController.h"

 

@interface PageAppViewController ()

 

@end

 

@implementation PageAppViewController

 

@synthesize pageContent=_pageContent;

@synthesize pageController=_pageController;

 

 

- (void)dealloc{

    [_pageContent release];

    [_pageController release];

    [super dealloc];

 

}

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

[self createContentPages];// 初始化所有数据

    // 设置UIPageViewController的配置项

    NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumbernumberWithInteger:UIPageViewControllerSpineLocationMin]

                                                       forKey:UIPageViewControllerOptionSpineLocationKey];

    

    // 实例化UIPageViewController对象,根据给定的属性

    self.pageController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl

                                                         navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal

                                                                        options: options];

    // 设置UIPageViewController对象的代理 

    _pageController.dataSource = self;

    // 定义“这本书”的尺寸

    [[_pageController view] setFrame:[[self view] bounds]];

    

    // 让UIPageViewController对象,显示相应的页数据。

    // UIPageViewController对象要显示的页数据封装成为一个NSArray。

    // 因为我们定义UIPageViewController对象显示样式为显示一页(options参数指定)。

    // 如果要显示2页,NSArray中,应该有2个相应页数据。

    MoreViewController *initialViewController =[self viewControllerAtIndex:0];//得到第一页

    NSArray *viewControllers =[NSArray arrayWithObject:initialViewController];

    [_pageController setViewControllers:viewControllers

                              direction:UIPageViewControllerNavigationDirectionForward

                               animated:NO

                             completion:nil];

    

    // 在页面上,显示UIPageViewController对象的View

    [self addChildViewController:_pageController];

    [[self view] addSubview:[_pageController view]];

}

 

// 初始化所有数据

- (void) createContentPages {

    NSMutableArray *pageStrings = [[NSMutableArray alloc] init];

    for (int i = 1; i < 11; i++){

        NSString *contentString = [[NSString alloc] initWithFormat:@"

Chapter %d

This is the page %d of content displayed using UIPageViewController in iOS 5.

", i, i];

        [pageStrings addObject:contentString];

    }

    self.pageContent = [[NSArray alloc] initWithArray:pageStrings];

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreaviewControllerAtIndexed.

}

 

// 得到相应的VC对象

- (MoreViewController *)viewControllerAtIndex:(NSUInteger)index {

    if (([self.pageContent count] == 0) || (index >= [self.pageContent count])) {

        return nil;

    }

    // 创建一个新的控制器类,并且分配给相应的数据

    MoreViewController *dataViewController =[[MoreViewController alloc] init];

    dataViewController.dataObject =[self.pageContent objectAtIndex:index];

    return dataViewController;

}

 

// 根据数组元素值,得到下标值

- (NSUInteger)indexOfViewController:(MoreViewController *)viewController {

    return [self.pageContent indexOfObject:viewController.dataObject];

}

 

#pragma mark- UIPageViewControllerDataSource

 

// 返回上一个ViewController对象

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

    

    NSUInteger index = [self indexOfViewController:(MoreViewController*)viewController];

    if ((index == 0) || (index == NSNotFound)) {

        return nil;

    }

    index--;

    // 返回的ViewController,将被添加到相应的UIPageViewController对象上。

    // UIPageViewController对象会根据UIPageViewControllerDataSource协议方法,自动来维护次序。

    // 不用我们去操心每个ViewController的顺序问题。

    return [self viewControllerAtIndex:index];

 

 

}

 

// 返回下一个ViewController对象

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

 

    NSUInteger index = [self indexOfViewController:(MoreViewController*)viewController];

    if (index == NSNotFound) {

        return nil;

    }

    index++;

    if (index == [self.pageContent count]) {

        return nil;

    }

    return [self viewControllerAtIndex:index];

 

 

}

 

@end

 
2.声明页对象,来根据UIPageViewController的调度来显示相应页内容。
 

MoreViewController.h

 

#import

 

@interface MoreViewController : UIViewController<</span>UIWebViewDelegate>{

 

}

 

@property (nonatomic, retain) UIWebView *myWebView;

@property (nonatomic, retain) id dataObject;

 

@end

 
MoreViewController.m
 

#import "MoreViewController.h"

 

@implementation MoreViewController

@synthesize myWebView=_myWebView;

@synthesize dataObject=_dataObject;

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

        // Custom initialization

    }

    return self;

}

 

- (void)dealloc{

    [_myWebView release];

    [super dealloc];

    

}

 

- (void) loadView{

    [super loadView];

    self.myWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];

 

}

 

- (void)viewDidLoad{

    [super viewDidLoad];

 

}

 

- (void) viewWillAppear:(BOOL)paramAnimated{

    [super viewWillAppear:paramAnimated];

    [self.myWebView loadHTMLString:_dataObject baseURL:nil];

    [self.view addSubview:self.myWebView];

   

}

 

- (void) viewWillDisappear:(BOOL)paramAnimated{

 

}

 

@end

 
三、总结
 
所有代码都已经写完了。其中也,写了相关的备注。
着重了解一下关键代码:
 
1.UIPageViewControllerDataSource协议
 
该协议主要有两个方法:

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

 

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

 
分别,用来提供
UIPageViewController对象的数据源。也就是说,
UIPageViewController对象通过该方法来调度显示的内容。
当然,别忘了设置:
_pageController.dataSource = self;
你懂的!
 
2.初始化时,显示适当内容
 

MoreViewController *initialViewController =[self viewControllerAtIndex:0];// 得到第一页

    NSArray *viewControllers =[NSArray arrayWithObject:initialViewController];

    [_pageController setViewControllers:viewControllers

                              direction:UIPageViewControllerNavigationDirectionForward

                               animated:NO

                             completion:nil];

    

    // 在页面上,显示UIPageViewController对象的View

    [self addChildViewController:_pageController];

    [[self view] addSubview:[_pageController view]];

 
3.数据必须先准备好。

[self createContentPages];// 初始化所有数据

 
希望对你有所帮助!

转载于:https://www.cnblogs.com/geek6/p/4158034.html

你可能感兴趣的文章
Tensorflow1.4 高级接口使用(estimator, data, keras, layers)
查看>>
Unix环境高级编程(四)数据系统文件和信息
查看>>
孟晓阳:IT运行监控系统设计与使用心得
查看>>
Navicat Premium 12.0.18安装与激活(转)
查看>>
Chart:Amcharts
查看>>
查看mysql服务器连接
查看>>
jquery是什么
查看>>
Yii之路(第八)
查看>>
[UWP小白日记-2]SQLite数据库DOME
查看>>
spring + Mybatis + pageHelper + druid 整合源码分享
查看>>
乐观锁 与 悲观锁 来解决数据库并发问题
查看>>
java.sql.SQLException: Field 'id' doesn't have a default value解决方案
查看>>
设置更改root密码 连接mysql mysql常用命令
查看>>
基于 Token 的身份验证
查看>>
C#从证书存储区读取证书
查看>>
NE555
查看>>
Docker 删除所有无名称的镜像(悬空镜像)
查看>>
URI -URL-URN区别
查看>>
Java 8 lambda表达式示例
查看>>
洛谷P3382 【模板】三分法(三分)
查看>>