当前位置: 代码迷 >> 综合 >> 【IOS 开发学习总结-OC-59】UI控件——UIPageControl 与UIPageViewController
  详细解决方案

【IOS 开发学习总结-OC-59】UI控件——UIPageControl 与UIPageViewController

热度:26   发布时间:2024-01-19 01:25:52.0

【IOS 开发学习总结-OC-58】UI控件——UIPageControl 与UIPageViewController

UIPageControl——页控件

是个由 N个小圆点组成的简单控件。——每个小圆点代表一个页面。

UIPageControl继承了 UIControl 基类。默认为活动控件。添加时,可以代码添加或者 IB 界面添加。
UIPageControl属性面板:
这里写图片描述

这里着重提一下defers page display,该属性对应UIPageControl的 defersCurrentPageDisplay 属性,若将该属性设为 YES,当用户点击该控件,使其跳转到某个新页时,该控件必须等到 updatePageIndicator 方法执行完成后,控件界面才会执行更新。

UIPageControl常与 UIScrollView 结合使用。当把UIScrollView的 pagingEnabled 设为 YES 后,此时与UIPageControl结合使用,这个时候UIPageControl控件的作用是:
- 显示当前UIScrollView正在显示第几页
- 单击UIPageControl控件后,控件发生 value change 事件时,程序控制UIScrollView滚动到指定页。

UIPageViewController

UIPageViewController属性面板:
这里写图片描述
其中的 doublesided 是控制是否双面显示的。

使用UIPageViewController的大致步骤

创建UIPageViewController对象(创建时可指定页面过渡方式,翻页方向,书脊位置等属性)——》若需要,可设置doublesided属性实现双面显示——》调用方法- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;设置控制器的初始页(——该页像一本默认打开的页面。这之后,用户可以根据初始页为基础,可向前或向后翻页。)——》为UIPageViewController设置 datasouce 属性(该属性必须实现UIPageViewControllerDataSource协议)。

Tip:
如果创建UIPageViewController时,指定了页面书籍位置在屏幕中间,那么该控制器初始就需要显示2个页面。——所以,必须为- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;方法传入2个viewController视图控制器作为初始的显示页面。

UIPageViewControllerDataSource协议中定义的方法

@protocol UIPageViewControllerDataSource <NSObject>@required
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
//使用UIPageViewController向前翻页时,会调用该方法返回UIViewController 作为前一个页面的视图控制器- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
//使用UIPageViewController向后翻页时,会调用该方法返回UIViewController 作为后一个页面的视图控制器@optional- (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); 
//返回PageViewController中的索引值 The selected item reflected in the page indicator.@end

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).
- (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.
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation;
//返回给定方向上的书脊位置 Returns the spine location for the given orientation.-(UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0);
//Returns the complete set of(整套的) supported interface orientations for the page view controller, as determined by the delegate(视delegate而定).
- (UIInterfaceOrientation)pageViewControllerPreferredInterfaceOrientationForPresentation:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(7_0);
//呈现优先的界面方向
@end