@qidiandasheng
2017-03-01T22:50:16.000000Z
字数 4476
阅读 3354
iOS界面开发
UIPageViewController作为子控制器加载在viewController上。作为容器可以很简单的实现图片轮播和翻书效果。
- (instancetype)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style
navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation
options:(nullable NSDictionary<NSString *, id> *)options NS_DESIGNATED_INITIALIZER;
上面初始化方法有3个参数:
style(过渡样式)
typedef NS_ENUM(NSInteger, UIPageViewControllerTransitionStyle) {
UIPageViewControllerTransitionStylePageCurl = 0, // Navigate between views via a page curl transition.
UIPageViewControllerTransitionStyleScroll = 1 // Navigate between views by scrolling.
};
0
翻书效果
1
滚动效果
navigationOrientation(导航方向)
typedef NS_ENUM(NSInteger, UIPageViewControllerNavigationOrientation) {
UIPageViewControllerNavigationOrientationHorizontal = 0,
UIPageViewControllerNavigationOrientationVertical = 1
};
0
水平导航方向
1
垂直导航方向
options(相应配置选项)
此参数是一个字典,只能以下面两个key组成字典
//这个key只有在style是翻书效果UIPageViewControllerTransitionStylePageCurl的时候才有作用, 它定义的是书脊的位置,值对应着UIPageViewControllerSpineLocation这个枚举项
UIKIT_EXTERN NSString * const UIPageViewControllerOptionSpineLocationKey;
//这个key只有在style是UIScrollView滚动效果UIPageViewControllerTransitionStyleScroll的时候才有作用, 它定义的是两个页面之间的间距(默认间距是0)
UIKIT_EXTERN NSString * const UIPageViewControllerOptionInterPageSpacingKey NS_AVAILABLE_IOS(6_0);
初始化完成后直接加入到当前UIViewController
的ChildViewController
:
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
@property (nonatomic, getter=isDoubleSided) BOOL doubleSided; // Default is 'NO'.
这个值默认为NO,表示初始页面是否是两面的。只有在style为UIPageViewControllerTransitionStylePageCurl
,options为@{UIPageViewControllerOptionSpineLocationKey:@(UIPageViewControllerSpineLocationMin)}
时必须设置为YES。
- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers
direction:(UIPageViewControllerNavigationDirection)direction
animated:(BOOL)animated
completion:(void (^ __nullable)(BOOL finished))completion;
UIPageViewController
是一个容器,那么就需要设置它的初始页面。
viewControllers
初始显示的UIViewController
示例,这是一个UIViewController
的数组,基本上都是只有一个元素的。如果doubleSided
为YES,则必须包含两个元素。
direction
这个参数跟初始化时navigationOrientation
有关,navigationOrientation
设置好了,好像这个参数怎么变都没差别。
animated
是否有动画
completion
设置初始页面完成的回调,只有在style为UIPageViewControllerTransitionStylePageCurl
才有回调。
@required
//返回上一个ViewController对象
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
//返回下一个ViewController对象
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
@optional
下面这两个是可选的。用于style
为UIPageViewControllerTransitionStyleScroll
的情况,主要用于显示轮播下面的小圆点。
//一共多少个小圆点
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0); // The number of items reflected in the page indicator.
//初始时选中的小圆点在第几个位置
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0); // The selected item reflected in the page indicator.
代理都是可选的。
@optional
//将要翻页或滚动时触发
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers NS_AVAILABLE_IOS(6_0);
//结束滚动或翻页的时候触发
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed;
这个方法是在style是UIPageViewControllerTransitionStylePageCurl 并且横竖屏状态变化的时候触发,我们可以重新设置书脊的位置,比如如果屏幕是竖屏状态的时候我们就设置书脊位置是UIPageViewControllerSpineLocationMin或UIPageViewControllerSpineLocationMax, 如果屏幕是横屏状态的时候我们可以设置书脊位置是UIPageViewControllerSpineLocationMid
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation __TVOS_PROHIBITED;
设置UIPageViewController支持的屏幕旋转类型,返回参数UIInterfaceOrientationMask
:一个枚举类型,用来定义屏幕支持的旋转的类型。
比如左旋转:UIInterfaceOrientationMaskLandscapeLeft
,支持左旋转屏幕和右旋转屏幕:UIInterfaceOrientationMaskLandscape
- (UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
设置优选方向 ,返回参数:UIInterfaceOrientation
,上下左右四个类型。
- (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;