简单介绍:
UIPageViewController是iOS 5 开始的分页控制器。
使用分页控制器(UIPageViewController)需要放置在一个父视图控制器中,在分页控制器下面还要有子视图控制器,每个子视图控制器对应图中的一个页面.
API里可以发现,属性绝大部分是只读的。而设置配置的信息需在的初始化方法中添加。
UIPageViewControllerNavigationOrientation : 导航翻页方向
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationOrientation) {
# 默认水平方向 ,左右滑动或翻页
UIPageViewControllerNavigationOrientationHorizontal = 0,
#垂直方向(上下滑动或翻页)
UIPageViewControllerNavigationOrientationVertical = 1
};
UIPageViewControllerSpineLocation
脊梁的位置不同,屏幕显示页数不同。
要设置脊柱位置,请将一个这些常量包含在NSNumber对象中,并将其设置为传递给方法的选项字典中的键的值。
UIPageViewControllerOptionSpineLocationKey
方法:initWithTransitionStyle:navigationOrientation:options:
typedef NS_ENUM(NSInteger, UIPageViewControllerSpineLocation) {
#默认设置,没有脊梁
#当style 是UIPageViewControllerTransitionStylePageCurl是无效的
UIPageViewControllerSpineLocationNone = 0, // Returned if 'spineLocation' is queried when 'transitionStyle' is not 'UIPageViewControllerTransitionStylePageCurl'.
# 一次显示一个视图控制器.
# 脊柱在屏幕的左边或顶边
# 从上往下翻页
UIPageViewControllerSpineLocationMin = 1, // Requires one view controller.
# 一次显示二个视图控制器.
# 脊柱在屏幕的中间
UIPageViewControllerSpineLocationMid = 2, // Requires two view controllers.
# 单页显示, 从下往上翻
# 脊柱在屏幕的右边或者下边
# 脊柱在屏幕的
UIPageViewControllerSpineLocationMax = 3 // Requires one view controller.
}; // Only pertains to 'UIPageViewControllerTransitionStylePageCurl'.
UIPageViewControllerNavigationDirection
视图控制器的切换方向
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationDirection) {
# 正向切换,切到下一页
UIPageViewControllerNavigationDirectionForward,
# 反向切换,切到前一页
UIPageViewControllerNavigationDirectionReverse
}; // For 'UIPageViewControllerNavigationOrientationHorizontal', 'forward' is right-to-left, like pages in a book. For 'UIPageViewControllerNavigationOrientationVertical', bottom-to-top, like pages in a wall calendar.
// 如果分页控制器设置为UIPageViewControllerNavigationOrientationHorizontal和forward,那页面从右到左,就书的翻页效果。
// 如果分页控制器设置为UIPageViewControllerNavigationOrientationVertical,那页面从下到上,页面就像一个挂历。
UIPageViewControllerTransitionStyle
两个页面过渡方式。
typedef NS_ENUM(NSInteger, UIPageViewControllerTransitionStyle) {
# 翻页
UIPageViewControllerTransitionStylePageCurl = 0, // Navigate between views via a page curl transition.
# 滑动
UIPageViewControllerTransitionStyleScroll = 1 // Navigate between views by scrolling.
};
// Key for specifying spine location in options dictionary argument to initWithTransitionStyle:navigationOrientation:options:.
// Value should be a 'UIPageViewControllerSpineLocation' wrapped in an NSNumber.
// Only valid for use with page view controllers with transition style 'UIPageViewControllerTransitionStylePageCurl'.
# 用在表示initWithTransitionStyle:navigationOrientation:options: 方法的options的字典参数的键(key),这个键表示脊柱定位。
# 表示UIPageViewControllerSpineLocation的数值被包装在NSNumber类型里
# 使用的分页控制器(page view controllers)的转场类型是UIPageViewControllerTransitionStylePageCurl才这个常量才有效
UIKIT_EXTERN NSString * const UIPageViewControllerOptionSpineLocationKey;
// Key for specifying spacing between pages in options dictionary argument to initWithTransitionStyle:navigationOrientation:options:.
// Value should be a CGFloat wrapped in an NSNumber. Default is '0'.
// Only valid for use with page view controllers with transition style 'UIPageViewControllerTransitionStyleScroll'.
# 一个键 在initWithTransitionStyle:navigationOrientation:options:方法的options的字典参数的键(key) ,表明页面间隔
# CGFloat类型的值,被包装在NSNumber的类型里。默认是0.
# 使用的分页控制器(page view controllers)的转场类型是UIPageViewControllerTransitionStyleScroll 才这个常量才有效
UIKIT_EXTERN NSString * const UIPageViewControllerOptionInterPageSpacingKey NS_AVAILABLE_IOS(6_0);
@protocol UIPageViewControllerDelegate, UIPageViewControllerDataSource;
NS_CLASS_AVAILABLE_IOS(5_0) @interface UIPageViewController : UIViewController {
}
1、初始化方法
# 最常用的构造方式
/**
*@param UIPageViewControllerTransitionStyle 转场类型
*@param UIPageViewControllerNavigationOrientation 导航方向
*@param options 一个字典,键是UIPageViewControllerOptionSpineLocationKey或者UIPageViewControllerOptionInterPageSpacingKey
*/
- (instancetype)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(nullable NSDictionary<NSString *, id> *)options NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
2、属性
# 代理
@property (nullable, nonatomic, weak) id <UIPageViewControllerDelegate> delegate;
# 数据源代理
@property (nullable, nonatomic, weak) id <UIPageViewControllerDataSource> dataSource; // If nil, user gesture-driven navigation will be disabled.
# 转场方式
@property (nonatomic, readonly) UIPageViewControllerTransitionStyle transitionStyle;
# 导航方向
@property (nonatomic, readonly) UIPageViewControllerNavigationOrientation navigationOrientation;
# 书脊位置
# 当转场类型是UIPageViewControllerTransitionStylePageCurl时,默认是UIPageViewControllerSpineLocationMin。否则是UIPageViewControllerSpineLocationNone。
@property (nonatomic, readonly) UIPageViewControllerSpineLocation spineLocation; // If transition style is 'UIPageViewControllerTransitionStylePageCurl', default is 'UIPageViewControllerSpineLocationMin', otherwise 'UIPageViewControllerSpineLocationNone'.
// Whether client content appears on both sides of each page. If 'NO', content on page front will partially show through back.
// If 'UIPageViewControllerSpineLocationMid' is set, 'doubleSided' is set to 'YES'. Setting 'NO' when spine location is mid results in an exception.
# 判断委托内容是否现在每个页面的两面。如果是NO,包含内容的页面的部分展示,前面展示而背面略过。
# 如果设置书脊位置是 UIPageViewControllerSpineLocationMid,doubleSided设置就是是yes。当 doubleSided 设置为0 ,则是书脊位置是中间的结果的一个异常。
@property (nonatomic, getter=isDoubleSided) BOOL doubleSided; // Default is 'NO'.
// An array of UIGestureRecognizers pre-configured to handle user interaction. Initially attached to a view in the UIPageViewController's hierarchy, they can be placed on an arbitrary view to change the region in which the page view controller will respond to user gestures.
// Only populated if transition style is 'UIPageViewControllerTransitionStylePageCurl'.
# 手势预配置数组去处理用户交互。首先附加到UIPageViewController的层级的一个view上。 他们可以放置在任意视图上,去改变分页控制器( page view controller) 回应用户手势的区域。
@property(nonatomic, readonly) NSArray<__kindof UIGestureRecognizer *> *gestureRecognizers;
@property (nullable, nonatomic, readonly) NSArray<__kindof UIViewController *> *viewControllers;
3、方法
// Set visible view controllers, optionally with animation. Array should only include view controllers that will be visible after the animation has completed.
// For transition style 'UIPageViewControllerTransitionStylePageCurl', if 'doubleSided' is 'YES' and the spine location is not 'UIPageViewControllerSpineLocationMid', two view controllers must be included, as the latter view controller is used as the back.
# 设置可视化控制器,可以选择添加动画。数组包含在动画完成后 ,将会可见视图控制器。
# 转场类型是UIPageViewControllerTransitionStylePageCurl,如果doubleSided是yes并且书脊位置不是UIPageViewControllerSpineLocationMid, 两个视图控制器必须包含在内。因为后者视图控制器作为后面。
- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;
4、UIPageViewControllerDelegate
@protocol UIPageViewControllerDelegate <NSObject>
# 可选
@optional
// Sent when a gesture-initiated transition begins.
# 当开始手势转场开始时会被发送。
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers NS_AVAILABLE_IOS(6_0);
// Sent when a gesture-initiated transition ends. The 'finished' parameter indicates whether the animation finished, while the 'completed' parameter indicates whether the transition completed or bailed out (if the user let go early).
# 当开始手势转场结束时会被发送。
/**
* @param finished 表示动画是否完成
* @param completed 表示 转场是否完成或者保释(如果用户更早释放页面)
*/
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed;
// Delegate may specify a different spine location for after the interface orientation change. Only sent for transition style 'UIPageViewControllerTransitionStylePageCurl'.
// Delegate may set new view controllers or update double-sided state within this method's implementation as well.
# 代理必须表明一个不同的书脊位置,为之后的界面方向改变准备。只有当转场类型是UIPageViewControllerTransitionStylePageCurl才会发送。
# 在这个方法的实现中,代理将会设置新的视图控制器或者更新双边状态
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation __TVOS_PROHIBITED;
- (UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
- (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
@end
5、 UIPageViewControllerDataSource
@protocol UIPageViewControllerDataSource <NSObject>
@required
# 必选方法
// In terms of navigation direction. For example, for 'UIPageViewControllerNavigationOrientationHorizontal', view controllers coming 'before' would be to the left of the argument view controller, those coming 'after' would be to the right.
// Return 'nil' to indicate that no more progress can be made in the given direction.
// For gesture-initiated transitions, the page view controller obtains view controllers via these methods, so use of setViewControllers:direction:animated:completion: is not required.
# 在导航方向的方面。例如,设置为
UIPageViewControllerNavigationOrientationHorizontal,视图控制器
# 返回nil,去表示在给定的方向,没有更多进展被创建
# 开始手势转场,页面视图控制器获取视图控制器是通过下面的这些方法,而不是setViewControllers:direction:animated:completion:获取。
# 向前翻页
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
# 向后翻页
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
@optional
# 可选方法
// A page indicator will be visible if both methods are implemented, transition style is 'UIPageViewControllerTransitionStyleScroll', and navigation orientation is 'UIPageViewControllerNavigationOrientationHorizontal'.
// Both methods are called in response to a 'setViewControllers:...' call, but the presentation index is updated automatically in the case of gesture-driven navigation.
# 如果下方二个方法都被实现、转场类型是UIPageViewControllerTransitionStyleScroll并且导航方向是UIPageViewControllerNavigationOrientationHorizontal页面的指示器将是可视的。
# 下方二个方法都被调用去响应'setViewControllers:...'的调用。但是在驱动手势导航的情况下 ,表示索引是自动更新。
// 在页面指示器中,items的个数被反应
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0); // The number of items reflected in the page indicator.
// 在页面指示器中,被选中的item被反映
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0); // The selected item reflected in the page indicator.
@end
传送门: