先看效果图:
分页控制器分两部分,
一是分页控制器,标题联动,分为内容页和导航标题视图,通过点击和滚动来同步状态。
#pragma mark - delegate
#pragma mark - 导航分类选择代理 点击标题滚动到相应的界面
- (void)XSNavChooseView:(XSNavCouseChoseView *)chooseView clickItem:(id)itemModel withIndex:(NSInteger)index {
CGFloat offsetX = index *self.view.frame.size.width;
[_backScrollView setContentOffset:CGPointMake(offsetX,0)animated:NO];
[self addChildVCWithIndex: index];
}
#pragma mark - ScroollView Delegate 拖动结束同步标题状态
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (![scrollView isKindOfClass:[UITableView class] ]) {
CGPoint offset = scrollView.contentOffset;
_navCoureCategoryView.currentBtnIndex = offset.x/self.view.frame.size.width;
NSInteger index = offset.x/self.view.frame.size.width;
[self addChildVCWithIndex: index];
}
}
所有的子控制器都是作为首页的自控制器,并在界面显示时候加载出来,支持标题的增减,不过至少保留一个首页
标题改变时候只改变导航标题
- (void)setNavTitleAry:(NSMutableArray *)navTitleAry {
_navTitleAry = navTitleAry;
self.navCoureCategoryView.modelAry = navTitleAry;
self.backScrollView.contentSize = CGSizeMake(self.view.frame.size.width * _navTitleAry.count, _backScrollView.frame.size.height);
[_backScrollView setContentOffset:CGPointMake(0,0)animated:NO];
// 导航栏数据变化移除子控制器
[self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![obj isKindOfClass:[XSRecommendVC class]]) {
//[self removeChild:obj];
[obj removeFromParentViewController];
}
}];
}
只有视图显示,滚动或者点击标题才添加到首页
- (void)addChildVCWithIndex:(NSInteger )index {
__block BOOL ishasVC = NO;
[self.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.title isEqualToString:_navTitleAry[index]]) {
ishasVC = YES;
}
}];
// 判断是否存在自控制器, 没有添加
if (ishasVC == YES) {
return;
}
NSString *titleName = _navTitleAry[index];
XSCourseCategoryVC *courseVC = [[XSCourseCategoryVC alloc] init];
courseVC.view.frame = CGRectMake(self.view.frame.size.width * index, 0, self.view.frame.size.width, _backScrollView.frame.size.height);
[_backScrollView addSubview:courseVC.view];
courseVC.title = titleName;
[self addChildViewController:courseVC];
}
二是分类选择器,支持collectionView的增删,拖动排序,原样是一个仿腾讯新闻的频道选择器,我在原来的基础上满足需求做了修改,原作者做了详细的说明,具体的请链接到:http://blog.csdn.net/u013282507/article/details/54374952
// 科目选择器
- (void)pushToChooseCourseCategoryVC {
typeof(self) weakSelf = self;
[[XLChannelControl shareControl] showChannelViewWithInUseTitles:_navTitleAry unUseTitles:_unUserTitleAry finish:^(NSArray *inUseTitles, NSArray *unUseTitles, NSString *title, BOOL isChange) {
if (isChange) {
weakSelf.navTitleAry = inUseTitles.mutableCopy;
weakSelf.unUserTitleAry = unUseTitles.mutableCopy;
}
if (title) {
// 点击 相应标题跳转到指定界面
[weakSelf.navTitleAry enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([title isEqualToString:obj]) {
[self addChildVCWithIndex:idx];
weakSelf.navCoureCategoryView.currentBtnIndex = idx;
CGFloat offsetX = idx *weakSelf.view.frame.size.width;
[weakSelf.backScrollView setContentOffset:CGPointMake(offsetX,0)animated:NO];
}
}];
}
}];
}