UIPageViewController-解析

丁翰海
2023-12-01
<span style="font-size:18px;">一、UIPageViewController概念

控件为我们提供了一种像翻书效果的一种控件。我们可以通过使用UIPageViewController控件,来完成类似图书一样的翻页控制方式。

二、使用UIPageViewController控件

先假象一下,一本书大概可以分为:1.每一页。2.每一页中相应的数据。
使用UIPageViewController控件,也是类似的两个构成部分。要有一个书的框架,来控制页;每一页的内容(一页即为一个对象</span><span style="font-family: Arial, Helvetica, sans-serif;font-size:18px;">)</span><pre name="code" class="objc">1.创建一个ViewController,包含一个UIPageViewController来控制显示,一个NSArray包括所有数据。

定义这个ViewController类,并使用UIPageViewController来管理每一页,并提供数据。
<span style="font-family: Arial, Helvetica, sans-serif;font-size:18px;">
</span>
#示例代码
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:[NSNumber numberWithInteger: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 %dThis 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

#页对象示例代码
<pre name="code" class="objc">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];// 初始化所有数据

希望对你有所帮助!

 

 

 类似资料: