XLPageViewController Save

一个开放、高度可定制化的分页视图控制器

Project README

title

目录:

特点:

  • 采用UICollectionView+UIPageViewController方案,高性能,低功耗。
  • 支持刷新,内置缓存(非复用)机制,节省内存。
  • 默认配置样式丰富,可实现大部分主流App样式。
  • 支持用户自定义标题样式。
  • 兼容全屏返回手势。

结构:

结构图

App举例:

App 示例
今日头条 image
腾讯新闻 image
澎湃新闻 image
爱奇艺 image
优酷 image
腾讯视频 image
网易新闻 image
人民日报 image

基本属性:

功能 示例
基本样式-标题正常显示 image
基本样式-标题显示在导航栏上 image
Segmented样式-标题正常显示 image
Segmented样式-标题显示在导航栏上 image
标题栏-居左 image
标题栏-居中 image
标题栏-居右 image
标题栏-自定义高度 image
标题-自定义宽度 image
标题-文字居上 image
标题-文字居下 image
标题-关闭标题颜色过渡 image
阴影动画-缩放 image
阴影动画-无 image
阴影末端形状-圆角 image
阴影末端形状-直角 image
阴影-居上 image
阴影-居中 image

特殊用法:

场景 示例
自定义标题Cell image
频道定制 image
多级嵌套 image
子View手势冲突 image
手动切换 image

使用:

1、创建方法

1.1 导入头文件

#import "XLPageViewController.h"

1.2 遵守协议

@interface ViewController ()<XLPageViewControllerDelegate, XLPageViewControllerDataSrouce>

1.3 创建外观配置类

注:config负责所有的外观配置,defaultConfig方法设定了默认参数,使用时可按需配置。 →Config属性列表

  XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];

1.4 创建分页控制器

注:需要把pageViewController添加为当前视图控制器的子视图控制器,才能实现子视图控制器中的界面跳转。

  XLPageViewController *pageViewController = [[XLPageViewController alloc] initWithConfig:config];
  pageViewController.view.frame = self.view.bounds;
  pageViewController.delegate = self;
  pageViewController.dataSource = self;
  [self.view addSubview:pageViewController.view];
  [self addChildViewController:pageViewController];

2、协议

2.1 XLPageViewControllerDelegate

//回调切换位置
- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index;

2.2 XLPageViewControllerDataSrouce

@required

//根据index创建对应的视图控制器,每个试图控制器只会被创建一次。
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index;
//根据index返回对应的标题
- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index;
//返回分页数
- (NSInteger)pageViewControllerNumberOfPage;

@optional

//标题cell复用方法,自定义标题cell时用到
- (__kindof XLPageTitleCell *)pageViewController:(XLPageViewController *)pageViewController titleViewCellForItemAtIndex:(NSInteger)index;

3、自定义标题cell

3.1 创建一个XLPageTitleCell的子类

#import "XLPageTitleCell.h"

@interface CustomPageTitleCell : XLPageTitleCell

@end

3.2 注册cell、添加创建cell

//需要先注册cell
[self.pageViewController registerClass:CustomPageTitleCell.class forTitleViewCellWithReuseIdentifier:@"CustomPageTitleCell"];
//自定义标题cell创建方法
- (XLPageTitleCell *)pageViewController:(XLPageViewController *)pageViewController titleViewCellForItemAtIndex:(NSInteger)index {
    CustomPageTitleCell *cell = [pageViewController dequeueReusableTitleViewCellWithIdentifier:@"CustomPageTitleCell" forIndex:index];
    return cell;
}

3.3 复写cell父类方法

//通过此父类方法配置标题cell是否被选中样式
- (void)configCellOfSelected:(BOOL)selected {

}

//通过此父类方法配置标题cell动画;type:区分是当前选中cell/将要被选中的cell;progress:动画进度0~1
- (void)showAnimationOfProgress:(CGFloat)progress type:(XLPageTitleCellAnimationType)type {
    
}

4、特殊情况处理

4.1 和子view手势冲突问题

pageViewController的子视图中存在可滚动的子view,例如UISlider、UIScrollView等,如果子view和pageViewController发生滚动冲突时,可设置子view的xl_letMeScrollFirst属性为true。

  UISlider *slider = [[UISlider alloc] init];
  slider.xl_letMeScrollFirst = true;
  [childVC.view addSubview:slider];

4.2 全屏返回手势问题

pageViewController和全屏返回手势一起使用时,需要将其它手势的delegate的类名添加到respondOtherGestureDelegateClassList属性中。当滚动到第一个分页时,向右滑动会优先响应全屏返回。以FDFullscreenPopGesture为例:

self.pageViewController.respondOtherGestureDelegateClassList = @[@"_FDFullscreenPopGestureRecognizerDelegate"];

5、注意事项

使用时需注意标题不要重复标题是定位ViewController的唯一ID。

更新

! 2019/07/29 解决快速滑动导致显示错乱问题
! 2019/07/31 修正scrollEnabled属性不生效问题
! 2019/08/01 处理预设selectedIndex如果超出屏幕时,标题选中位置错乱问题
! 2019/08/03 处理当标题栏样式是Segmented时,点击标题切换慢的问题
+ 2019/08/03 添加UIViewController扩展标题属性,避免因title改变导致的异常
! 2019/08/19 解决因滑动距离过大,导致出现空白界面问题
! 2019/09/05 解决刷新方法可能造成的闪退问题
! 2019/09/23 解决使用多级嵌套时,可能出现界面错乱问题
! 2019/09/23 解决滑动切换时,标题栏可能会再一瞬间出现动画失效的问题
! 2020/03/19 解决滑动距离小,自动回弹后导致标题点击失效问题
! 2020/03/25 解决从网络获取标题,刷新后阴影位置没有更新问题
! 2020/04/02 解决设置selectedIndex时,可能出现底部阴影显示出错问题
! 2020/04/23 解决设置selectedIndex后,代理方法可能不执行问题
+ 2020/05/06 添加全屏手势解决方案

其他

Open Source Agenda is not affiliated with "XLPageViewController" Project. README Source: mengxianliang/XLPageViewController
Stars
369
Open Issues
18
Last Commit
2 years ago

Open Source Agenda Badge

Open Source Agenda Rating