最近公司需要做类似分段选择的功能,自己研究了下,基本完成了业务需求,抽时间把这个控制器封装了一遍:
1.下面先演示下实际效果:
2.使用的话比较简单,在封装控制器中提供了一个类方法
// 通过传入标题数组和控制器数组来创建当前的根控制器
- (id)initWithTitleArray:(NSArray *)titleArray controllersArray:(NSArray *)controllersArray;
// 下面的代码为演示方法
- (IBAction)jumpToSelectView:(id)sender {
NSArray *titleArray = @[@"第一页",@"第二页222",@"第三页3",@"第四页44444"];
NSMutableArray *controllersArray = [NSMutableArray array];
for (int i = 0; i < 4; i++) {
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = RandomColor;
[controllersArray addObject:vc1];
}
CWSegmentRootController *rootVc = [[CWSegmentRootController alloc] initWithTitleArray:titleArray controllersArray:controllersArray];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVc];
[self presentViewController:nav animated:YES completion:nil];
}
3.下面说下具体实现过程
. 首先整个视图分为上面的标题视图以及下面的内容视图,标题视图主要分为标题Button和下面的指示器,具体的创建是根据标题数组的数量确定的:
/**
* 设置顶部的标签栏
*/
- (void)setupTitlesView
{
// 标签栏整体
UIView *titlesView = [[UIView alloc] init];
titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0];
titlesView.frame = CGRectMake(0, 64, self.view.frame.size.width, 35);
[self.view addSubview:titlesView];
self.titlesView = titlesView;
// 底部的红色指示器
UIView *indicatorView = [[UIView alloc] init];
indicatorView.backgroundColor = [UIColor redColor];
indicatorView.tag = -1;
CGFloat indicatorViewW = self.view.frame.size.width/(self.titlesArray.count);
CGFloat indicatorViewH = 2;
CGFloat indicatorViewY = titlesView.frame.size.height - indicatorViewH;
indicatorView.frame = CGRectMake(0, indicatorViewY, indicatorViewW, indicatorViewH);
self.indicatorView = indicatorView;
// 内部的子标签
CGFloat width = titlesView.frame.size.width / self.titlesArray.count;
CGFloat height = titlesView.frame.size.height;
for (NSInteger i = 0; i<self.titlesArray.count; i++) {
UIButton *button = [[UIButton alloc] init];
button.tag = i;
button.frame = CGRectMake(i*width, 0, width, height);
[button setTitle:self.titlesArray[i] forState:UIControlStateNormal];
// [button layoutIfNeeded]; // 强制布局(强制更新子控件的frame)
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
[titlesView addSubview:button];
// 默认点击了第一个按钮
if (i == 0) {
button.enabled = NO;
self.selectedButton = button;
// 让按钮内部的label根据文字内容来计算尺寸
[button.titleLabel sizeToFit];
CGRect newFrame = indicatorView.frame;
newFrame.size.width = button.titleLabel.frame.size.width;
self.indicatorView.frame = newFrame;
CGPoint newCenter = self.indicatorView.center;
newCenter.x = button.center.x;
self.indicatorView.center = newCenter;
}
}
[titlesView addSubview:indicatorView];
}
. 然后是添加内容容器ScrollView,这里注意设置ContentSize(根据控制器数组的数量确定的)
/**
* 底部的scrollView
*/
- (void)setupContentView
{
// 不要自动调整inset
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView *contentView = [[UIScrollView alloc] init];
contentView.backgroundColor = [UIColor lightGrayColor];
// contentView.frame = self.view.bounds;
contentView.frame = CGRectMake(0, CGRectGetMaxY(self.titlesView.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(self.titlesView.frame));
contentView.delegate = self;
contentView.pagingEnabled = YES;
[self.view insertSubview:contentView atIndex:0];
contentView.contentSize = CGSizeMake(contentView.frame.size.width * self.childViewControllers.count, 0);
self.contentView = contentView;
// 添加第一个控制器的view
[self scrollViewDidEndScrollingAnimation:contentView];
}
. 最后需要将传入的多个控制器传入根控制器中:
/**
* 初始化子控制器
*/
- (void)setupChildVces
{
for (UIViewController *vc in self.controllersArray) {
[self addChildViewController:vc];
}
}
4.接下来需要监听标题按钮的点击以及内容视图的滚动
// 标题按钮点击时需要滚动视图以及让指示器移动
- (void)titleClick:(UIButton *)button
{
// 修改按钮状态
self.selectedButton.enabled = YES;
button.enabled = NO;
self.selectedButton = button;
// 动画
[UIView animateWithDuration:0.25 animations:^{
CGRect newFrame = self.indicatorView.frame;
newFrame.size.width = button.titleLabel.frame.size.width;
self.indicatorView.frame = newFrame;
CGPoint newCenter = self.indicatorView.center;
newCenter.x = button.center.x;
self.indicatorView.center = newCenter;
}];
// 滚动
CGPoint offset = self.contentView.contentOffset;
offset.x = button.tag * self.contentView.frame.size.width;
[self.contentView setContentOffset:offset animated:YES];
}
5.最后需要监听ScrollView的代理方法
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
// 当前的索引
NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
// 取出子控制器
UIViewController *vc = self.childViewControllers[index];
vc.view.frame = CGRectMake(scrollView.contentOffset.x, 0, scrollView.frame.size.width, scrollView.frame.size.height);
[scrollView addSubview:vc.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self scrollViewDidEndScrollingAnimation:scrollView];
// 点击按钮
NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
[self titleClick:self.titlesView.subviews[index]];
}