2.26 Enabling Paging with UIPageViewController

来源:互联网 发布:天猫店和淘宝店哪个好 编辑:程序博客网 时间:2024/05/22 02:56

实现看书翻书效果
照例,有模版,创建后会发现有以下几个类

在我电脑上创建出来的是storyboard文件,跟书上的描述有些差异

Delegate
app delegate 只是简单的创建了rootViewController的实例,并呈现给用户,工程中有对应iPad、iPhone的.xib文件。

RootViewController
创建UIPageViewController实例,并加入到childViewController中。所以这个控制器的UI实际上是这两者的组合。这个控制器实现的UIPageViewControllerDelegate协议,并作为pageviewController的代理。

DataViewController
对page view controller中的每一页,都有一个这个类的实例呈现给用户,这个类是UIViewController的之类。

ModelController
这个控制器是用来给Page View controller 提供数据的,所以他只需从NSObject那里派生就可以,不过还需要实现UIPageViewControllerDataSource Protocol。

到这里,我们看到RootViewController是pageViewController的代理,而ModelController为pageViewController提供数据。接下来我们来看看他是怎么工作的。

 

UIPageViewControllerDelegate 代理中有两个重要的方法

- (void)pageViewController:(UIPageViewController *)pageViewController
didFinishAnimating:(BOOL)finished
previousViewControllers:(NSArray *)previousViewControllers
transitionCompleted:(BOOL)completed;


- (UIPageViewControllerSpineLocation)
pageViewController:(UIPageViewController *)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation;

第一个是在翻页时调用。不管是翻过来还是翻过去,还是翻到一半又翻回来。有实现翻页的completed值为YES, 否则为NO。

第二个是在设备翻转时调用。返回值类型如下:
enum {
UIPageViewControllerSpineLocationNone = 0,
UIPageViewControllerSpineLocationMin = 1,//需要一个视图
UIPageViewControllerSpineLocationMid = 2,//需要两个个视图
UIPageViewControllerSpineLocationMax = 3//需要一个视图
};
typedef NSInteger UIPageViewControllerSpineLocation;

UIPageViewControllerSpineLocationMin 表示只需要显示一页
UIPageViewControllerSpineLocationMid 表示需要显示两页,中间有分隔线


UIPageViewControllerDataSource //数据源中的两个方法

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

第一个方法将在当前控制器需要知道前一个控制器时被调用。这会在用户觉得翻到下页时触发。(这里我觉得有点蒙)第二个方法将在pageViewController在某一页被翻过去之后需要确定下一页显示时触发。
书上原文是:
The first method gets called when the page view controller already has a view controller on the screen and needs to know which previous view controller to render. This happens when the user decides to flip to the next page. The second method is called when the page view controller needs to figure out which view controller to display after the view controller that is being flipped.

当然了,上面的这些这么复杂的东西,Xcode已经帮我们做好了。我们需要做的是如何在提供显示的文本,以及如何显示这些文本。


---------------------
在我们看的书中,通常有几百上千页,他不可能创建成百上千个实例吧,或是在翻页的时候把旧的实例销毁,新的实例加进来?我在模版中看到这样一句话[storyboard instantiateViewControllerWithIdentifier:@"DataViewController"],这应该是类似UITableView那边的重用机制吧

- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{  
    // Return the data view controller for the given index.
    if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
        return nil;
    }
   
    // Create a new view controller and pass suitable data.
    DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"DataViewController"];
    dataViewController.dataObject = self.pageData[index];
    return dataViewController;
}

----------------------