PageViewController和UIPageControl

以前经常把这两个东西当成一回事, PageViewController像电子书那样,一页之中可以放几个childViewcontroller,  然后左右翻,当前frame显示几个viewcontroller。 UIPageControl配合scrollView是用于左右滑动翻页的,当前frame只显示一个viewcontroller。

Page View Controllers
你使用一个page view controller用page by page的方式来展示内容。一个page view controller管理一个self-contained视图架构。这个架构的父视图由page View controller管理,并且子视图由你提供的view Controllers管理。

一,解析Page View Controller
一个page view controller有一个单独的视图,是你的内容的存放的地方。
下图显示了page view interface的结构:最外面的棕色视图是父视图控制器,不是page view controller自身。page view controller没有自己的UI;然而,它对它的children做一个翻页效果。

二,page view controller interface的对象:
一个可选的代理,一个可选的数据源,一组current view controllers,一组手势识别者

数据源按需提供viewControllers。
委托提供响应基于手势的导航和方向更改。
view Controllers的数组包含当前要显示的content view controller。数组中的items的数量基于传递给page view controller的选项。
手势识别者只有在数据源被指定时才存在。这些手势识别者使用户通过轻击、flicking(快速滑动)或拖动翻页。

三,创建page view controller interface:
page view controller的view可以被缩放和嵌入到一个视图层次架构中。这意味着,不像一个导航控制器或tab bar controller,page view controller可以在广阔的范围内使用,而不是一些特别的情况。

1,通过stroyboard创建page view controller: Page-Based Application Xcode模版创建一个新的带page view controller的工程。
要添加一个page view controller到一个已经存在的storyboard:
1)拖动一个page view controller,并添加一个page view controller scene 到你的storyboard.
2)在属性检查器中,设置适当的选项。
3)可选地,设置一个委托,数据源
4)。。。

2,通过代码创建一个page view controller:
1)通过initWithTransitionStyle:navigationOrientation:options:方法创建一个page view controller
2)可选地,设置一个代理、数据源
3)设置初始的content view controllers
4) 在屏幕上显示page view controller的view。

3,设置最初的view controller:无论你是通过代码创建还是通过IB创建了一个page view controller,你都需要在将其显示到屏幕之前指定一个初始的view controller。
调用setViewControllers:direction:animated:completion:方法。

如果page view controller没有initial view controller,它的shouldAutorotateToInterfaceOrientation:返回NO,并抛出一个异常。

四,自定义初始化行为:
在初始化时,你传递给initWithTransitionStyle:navigationOrientation:options:方法的参数值和选项来自定义一个page view controller。在初始化后,这些属性都是只读的。你可以自定义:
1)The direction in which navigation occurs.
2)The location of the spine.(书脊的位置)
3)The transition style. In iOS 5, the only valid transition style is UIPageViewControllerTransitionStylePageCurl.

例如,

Listing 3-1  Customizing a page view controller
NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMid] forKey:UIPageViewControllerOptionSpineLocationKey];

UIPageViewController *pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];

五、在运行时使用代理自定义行为:
UIPageViewControllerDelegate协议:可以在设备方向更改时和用户导航到一个新页时执行动作,并且它可以更新书脊的位置来响应方向的转变。

六、通过数据源提供内容:
数 据源的方法被当前显示的view controller调用,并返回它之前和之后的view controller。为了简化查找前面和后面的viewcontroller,你可以在你的view controller里存储额外的信息,例如一个page number。
如果指定了数据源,page view controller将关联手势识别到它的view。这些手势通过gestureRecognizers属性访问。

要移动手势识别到另一个view,传递gestureRecognizers属性的值给目标view的addGestureRecognizer:方法。

例如,如果你的page view controller没有充满屏幕,你将手势识别传递给其superview,可以更好地翻页,而不用必须在page view controller内翻页。

七,通过设置current view controller提供内容:
setViewControllers:direction:animated:completion:方法直接控制显示哪些内容。
如果你没有设置数据源,你需要提供UI来移动pages,例如前进和后退按钮。手势驱动的导航只有在在你提供了数据源的时候才可用。

八,特别考虑Right-to-Left和Bottom-to-Top Content:
要通过数据源,使用一个page view controller来显示一个right-to-left或bottom-to-top内容,只需要反转下面两个方法的实现:
1)在你的数据源中,实现pageViewController:viewControllerBeforeViewController: 并返回之后的的viewController
2)pageViewController:viewControllerAfterViewController:返回前面的viewController>

并且,你一般要设置书脊的位置为UIPageViewControllerSpineLocationMax

时间: 2024-07-30 20:59:53

PageViewController和UIPageControl的相关文章

iOS开发 首次启动显示用户引导,第二次启动直接进入App,UIScrollView,UIPageControl,NSUserDefaults

首先创建一个引导图的控制器类 UserGuideViewController.h和UserGuideViewController.m #import <UIKit/UIKit.h> #import "firstViewController.h" #define WIDTH self.view.frame.size.width #define HEIGHT self.view.frame.size.height @interface UserGuideViewControll

IOS-OC-基本控件之UIPageControl

UIPageControl(页面控制器,就是桌面的那些小点点,每个点代表一个界面) 父类是 UIControl. IOS开发中常用的基本控件,主要和UIScrollView一起使用,比较常用的就是有些APP主页面会有一些图片自动转换,也可以用而人为的转换,本文主要是列出常用的属性及方法(注XCode版本为7.2) 属性如下: @property(nonatomic) NSInteger numberOfPages;          //总页数,默认0页 @property(nonatomic)

scrollView实现基础中心点缩放及与UIPageControl结合使用

一般来说scrollView与UIPageControl都是结合使用,因为在使用滚动视图的时候 ,使用UIPageControl,用户可以 清楚 的明白显示的内容有多少页,如果 没有的话,总不能让用户一个个的去数.用户体验效果不理想. 一般来说,在滑动scrollView的时候,UIPageControl的选中点的位置也会有相应的变化.当然,反之来说,改变选中点的位置,相应的scrollView显示的内容也会产生变化 . 因为我这边需要实现的效果是:最后一张向后滑动之后,会自动跳到第一张显示的内

UIPageControl

UIPageControl用于显示滑动视图实际拥有的页数 UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 100, 100, 30); page.background = [UIColor redColor]; page.numberOfPages = 5;  //设置拥有的页数 page.currentPage = 2; //设置当前显示第几个点 page.pageIndicatorTintC

通过UIScrollView和UIPageControl结合制作相册

1.创建UIScrollView视图: UIScrollView *aScrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease]; aScrollView.backgroundColor = [UIColor redColor]; [self.view addSubview:aScrollView]; aScrollView.contentSize = CGSizeMake(CGRectGetW

UIPageControl 与 UIScrollView

#import "MainViewController.h" #define WIDTH self.view.frame.size.width #define HEIGHT self.view.frame.size.height @interface MainViewController ()<UIScrollViewDelegate> @property(nonatomic, retain)UIScrollView *scrollView; @end @implement

UIScrollView 按页面滚动视图 UIPageControl

1.UIScrollView是可以滚动的view,UIView本?身不能滚动,子类UIScrollview拓展了滚动?方?面的功能. 2.UIScrollView是所有滚动视图的基类.以后的UITableView,UITextView等视图都是继承于该类. 3.UIScrollview主要专?长于两个?方?面: (1).滚动:contentSize?大于frame.size的时候,能够滚动. (2).缩放:?自带缩放,可以指定缩放倍数. UIScrollView滚动的相关属性 1.content

UI_UIScrollView/UIpageControl

创建UIScrollView @property(nonatomic, retain) UIScrollView *scrollView; @property(nonatomic, retain) UIImageView *opeImageView; @property (nonatomic, retain)UIPageControl *pageControl; #pragma mark - 创建滑动视图 UIScrollView - (void)createScrollView { self.

UI开发----UIScrollView和UIPageControl简单模拟滚动视图

//  Created By 郭仔 //================================================== 师傅领进门,修行在个人!自学才是王道! //================================================== UIScrollView: UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 50, 200, 200)]