@interface ViewController ()<UIScrollViewDelegate>
{
// 标题
UIScrollView * _titleScrollView;
// 内容
UIScrollView * _contentScrollView;
// 存储标题的数组
NSArray * _titleArray;
// 记录用户上一个选中的标题
NSInteger _lastTag;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.添加标题滚动视图
[self addTitleScrollView];
// 2.内容滚动视图
[self addContentScrollView];
}
-(void)addTitleScrollView{
// 1.设置显示的标题滚动视图
_titleArray = @[@"头条",@"直播",@"娱乐",@"游戏",@"生活",@"阿怡",@"外卖",@"体育",@"科技",@"动漫"];
CGFloat titleX = 0;
CGFloat titleY = 0;
CGFloat titleW = SCR_W;
CGFloat titleH = 50;
_titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(titleX, titleY, titleW, titleH)];
_titleScrollView.backgroundColor = [UIColor lightGrayColor];
_titleScrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:_titleScrollView];
// 2.设置标题
CGFloat labelW = 80;
_titleScrollView.contentSize = CGSizeMake(labelW * _titleArray.count, 0);
for (int i = 0; i < _titleArray.count; i++) {
//
CGFloat labelX = labelW * i;
CGFloat labelY = 0;
CGFloat labelH = titleH;
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelW, labelH)];
// 文字居中
label.textAlignment = NSTextAlignmentCenter;
// 将文字赋给标签
label.text = _titleArray[i];
label.font = [UIFont systemFontOfSize:INIT_TITLE_SIZE];
label.tag = 100 + i;
label.userInteractionEnabled = YES;
// 默认选中
if (i == 0) {
// 默认的label文字颜色
label.textColor = [UIColor greenColor];
label.font = [UIFont systemFontOfSize:SELE_TITLE_SIZE];
_lastTag = 100 + i;
}
// 添加点击手势
UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
// 给标签添加手势
[label addGestureRecognizer:tapGes];
[_titleScrollView addSubview:label];
}
}
-(void)tapGes:(UITapGestureRecognizer *)sender{
// 获取label的tag值
// sender.view 获取到当前控件上 即label上
NSInteger indexTag = sender.view.tag;
// 标签状态的处理
if (indexTag == _lastTag) {// 重复点击同一个 就不操作
return;
}else{
// 修改当前选中的label 标签
UILabel * nowLabel = (UILabel *)sender.view;
nowLabel.textColor = [UIColor greenColor];
nowLabel.font = [UIFont systemFontOfSize:SELE_TITLE_SIZE];
// 修改上一个lable的状态
UILabel * lastLabel = [self.view viewWithTag:_lastTag];
lastLabel.textColor = [UIColor blackColor];
lastLabel.font = [UIFont systemFontOfSize:INIT_TITLE_SIZE];
// 将当前标签作为上一个标签
_lastTag = indexTag;
// 移动内容滚动视图
CGFloat offSetX = SCR_W * (indexTag - 100);
[_contentScrollView setContentOffset:CGPointMake(offSetX, 0) animated:YES];
}
}
//
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// 获取用户滑动到的页数
// 加了100后 页数的值就相当于label的tag值
NSInteger indexPage = scrollView.contentOffset.x/SCR_W +100;
if (indexPage == _lastTag) { // 用户没有切换界面
// 仍在当前界面
return;
}
// // 获取用户当前选中的label
// UILabel * nowLabel = [self.view viewWithTag:indexPage];
// 修改当前选中的label 标签
UILabel * nowLabel = [self.view viewWithTag:indexPage];
nowLabel.textColor = [UIColor greenColor];
nowLabel.font = [UIFont systemFontOfSize:SELE_TITLE_SIZE];
// 修改上一个lable的状态
UILabel * lastLabel = [self.view viewWithTag:_lastTag];
lastLabel.textColor = [UIColor blackColor];
lastLabel.font = [UIFont systemFontOfSize:INIT_TITLE_SIZE];
_lastTag = indexPage;
// 让title跟随着内容滑动
if (indexPage > 102) {
CGFloat offSetX = (indexPage - 102) * 80;
[_titleScrollView setContentOffset:CGPointMake(offSetX, 0) animated:YES];
}
}
-(void)addContentScrollView{
CGFloat scrollViewX = 0;
CGFloat scrollViewY = CGRectGetMaxY(_titleScrollView.frame);
CGFloat scrollViewW = SCR_W;
CGFloat scrollViewH = SCR_H;
// 展示范围
_contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(scrollViewX, scrollViewY, scrollViewW, scrollViewH)];
// 显示活动范围
_contentScrollView.contentSize = CGSizeMake(SCR_W * _titleArray.count, 0);
_contentScrollView.delegate = self;
_contentScrollView.pagingEnabled = YES; // 分页
// 类名方法 可以不用导入头文件
NSArray * clsArray = @[@"HeardViewController",@"TwoViewController",@"HeardViewController",@"HeardViewController",@"HeardViewController",@"HeardViewController",@"HeardViewController",@"HeardViewController",@"HeardViewController",@"HeardViewController"];
for (int i = 0; i < clsArray.count ; i++) {
Class clas = NSClassFromString(clsArray[i]);
UIViewController * viewController = [[clas alloc] init];
UIView * view = viewController.view;
CGRect fram = view.frame;
fram.origin.x = SCR_W * i;
view.frame = fram;
[_contentScrollView addSubview:view];
}
[self.view addSubview:_contentScrollView];
}
@end
效果