DDKPageViewController
这是使用 UIPageViewController 封装实现的分页控件,在 UIPageViewController 的基础上进行了一些改进,以满足日常简单的分页需求。
导入
CocoaPods 方法
pod 'DDKPageViewController'
// 在使用的类中导入头文件
#import <DDKPageViewController.h>
手动导入
请下载相关的文件并将它们导入到您的项目中:文件下载链接
// 在使用的类中导入头文件
#import "DDKPageViewController.h"
使用方法
使用方法基本上与 UIPageViewController 一致,但需要使用 pageSource
中的代理方法来代替 UIPageViewController
中的原有 pageViewController: viewControllerBeforeViewController:
和 pageViewController: viewControllerAfterViewController :
方法。使用方法如下:
- 初始化
// 使用UIPageViewController相同的创建方法
DDKPageViewController *pageViewController = [[DDKPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
// 设置控件大小
pageViewController.view.frame = pageViewFrame;
// 设置数据代理(替代dataSource中的部分方法)
pageViewController.pageSource = self;
[self.view addSubView:pageViewController.view];
[pageViewController didMoveToParentViewController:self];
- 代理方法
#pragma mark - DDKPageViewControllerPageSource
// 必选方法,需要在此方法中返回指定索引值的UIViewController,返回nil代表当前页索引无效
- (UIViewController *)pageView:(DDKPageViewController *)pageView loadIndex:(NSInteger)index {
UIViewController *viewController = nil;
if (0 == index%2) {
FirstViewController *first = [FirstViewController new];
first.index = index;
viewController = first;
}
else {
SecondViewController *second = [SecondViewController new];
second.index = index;
viewController = second;
}
return viewController;
}
// 可选方法,用于通知页索引值的更新
- (void)pageView:(DDKPageViewController *)pageView indexUpdate:(NSInteger)index {
self.title = [NSString stringWithFormat:@"%zd",index];
}
- 翻页
使用属性 currentPageIndex
或者 setCurrentPageIndex 方法
设置当前需要显示的 UIViewController 的页索引值,DDKPageViewController 将通过调用代理方法 pageView: loadIndex:
获取并设置给定索引值的 UIViewController。
#pragma mark - Action
- (IBAction)lastButtonClickAction:(id)sender {
_pageViewController.currentPageIndex -= 1;
}
- (IBAction)nexButtonClickAction:(id)sender {
_pageViewController.currentPageIndex += 1;
}
说明
该控件是我在项目中使用UIPageViewController时对UIPageViewController的封装和处理,可以满足常见的分页功能。在使用过程中遇到问题,欢迎通过此链接联系我联系,若有好的建议,欢迎告知。