1.效果:
2.概要:
这个组件主要是我项目中使用的,拿出来就开源了,可能封装性并不是很强,因为好的封装需要很多附加代码,个人不太喜欢干这个。所以你想拿来直接用也是可以的,代码里面写了很多演示用法。这个组件最主要的目的是提供一种实现方式,而这种方式核心代码量相对较少,易于学习和自己扩展实现,这才是本项目的核心。
3.实现:
XQPageController分页控制器组件主要使用了系统的组件UIPageViewController,在此组件的基础上添加每个栏目的控制器,每个控制器可以单独管理自己,也有很多“网易新闻”栏目是使用一个外部控制器里面嵌套控制器,这些子控制器时添加在自定义的scrollview上的,比如WMPageControllerDemo
1.实现数据源代理方法UIPageViewControllerDataSource
,通过子类协议[self.dataSource viewcontrollerWithIndex:index]
实现返回对应的控制器方法,在数据源返回对应控制器
- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerBeforeViewController:(UIViewController *)vc {
NSInteger index = self.nextIndex - 1;
return [self controllerWithIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerAfterViewController:(UIViewController *)vc {
NSInteger index = self.nextIndex + 1;
return [self controllerWithIndex:index];
}
- (UIViewController *)controllerWithIndex:(NSInteger)index {
if (index < [titles count] && index >= 0) {
UIViewController *controller = [self.dataSource viewcontrollerWithIndex:index];
controller.index = index;
controller.pageDelegate = self;
return controller;
}
return nil;
}
2.UIPageViewControllerDelegate
代理方法返回UIPageViewController
对应的index,以及下一个index和变换状态等
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
self.inTransition = YES;
self.nextIndex = pendingViewControllers[0].index;
if ([self.dataSource respondsToSelector:@selector(pageViewController:willTransitionToViewControllers:)] && ![self isEqual:self.dataSource]) {
[self.dataSource pageViewController:pageViewController willTransitionToViewControllers:pendingViewControllers];
}
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
self.inTransition = NO;
}
- (void)pageViewController:(id<ScrollPageViewControllerProtocol>)pageViewController didShowViewController:(UIViewController *)controller atIndex:(NSInteger)index {
self.nextIndex = NSNotFound;
self.currentIndex = index;
if ([self.dataSource respondsToSelector:@selector(pageViewController:didShowViewController:atIndex:)] && ![self.dataSource isEqual:self]) {
[self.dataSource pageViewController:pageViewController didShowViewController:controller atIndex:index];
}
}
3.还有上面的menuBar视图,这里使用的是FDSlideBar,对代码进行的稍微的修改。添加到UIPageViewController
上。
4.最主要的一点,协同控制
当滑动pagecontroller
的时候,slidebar
需要做相应的item
切换,点击slidebar
的时候,pagecontroller
也要切换到对应的位置。
先说滑动pagecontroller
的时候:
通过UIPageViewControllerDelegate
获取index,通过UIPageViewControlle
的scrollview
获取偏移量,更改slidebar的frame
同时,监听currentindex的改变,切换slidebar的item
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"currentIndex"]) {
NSUInteger changenew = [change[@"new"] integerValue];
NSUInteger changeold = [change[@"old"] integerValue];
if (changeold != changenew) {
[self.slideBar selectSlideBarItemAtIndex:changenew];
}
}
}
接下来看是slidebar改变pagecontroller
的实现,在slidebar里增加回调方法,回调currentIndex,然后pagecontroller
切换到对应位置
- (void)setupSlideBar {
FDSlideBar *slide = self.slideBar;
slide.backgroundColor = [UIColor whiteColor];
slide.itemColor = [UIColor blackColor];
slide.itemSelectedColor = [UIColor redColor];
slide.sliderColor = [UIColor redColor];
slide.showMenuButton = _showMore;
slide.menuButtonSelectedTitleColor = [UIColor colorWithWhite:.5 alpha:1];
slide.menuButtonImage = [UIImage imageNamed:@"gonggao_customized.png"];
slide.menuButtonSelectedImage = [UIImage imageNamed:@"gonggao_customized.png"];
slide.menuButtonTitleColor = [UIColor colorWithWhite:0.5 alpha:1];
// callBack
__weak typeof(self) weakSel = self;
[slide slideBarItemSelectedCallback:^(NSUInteger idx) {
if (idx != weakSel.currentIndex) {
[weakSel setToIndex:idx];
}
}];
}
/// 切换到某个index
- (void)setToIndex:(NSInteger)index {
self.nextIndex = NSNotFound;
if ([titles count]) {
// 判断index,防止传入异常数据导致角标越界
if (index >= [titles count]) index = [titles count] - 1;
if (index < 0) index = 0;
__weak typeof(self) weakSelf = self;
UIViewController *list = [self controllerWithIndex:index];
[self.pageViewController setViewControllers:@[list] direction:(index > self.currentIndex ?UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse) animated:YES completion:^(BOOL finished){
weakSelf.currentIndex = index;
}];
}
}
主要的讲完了,剩下的就是一些琐碎的补充了,具体看代码
代码:XQPageController