#pragma mark - 创建控制器内容的scrollView
- (void)setUpChildView
{
//创建UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] init];
CGFloat X = 0;
CGFloat Y = CGRectGetMaxY(self.titleView.frame);
CGFloat W = XFJUIScreenW;
CGFloat H = XFJUIScreenH - Y;
//设置尺寸
scrollView.frame = CGRectMake(X, Y, W, H);
//添加到控制器的view中
[self.view addSubview:scrollView];
//设置背景颜色
scrollView.backgroundColor = [UIColor clearColor];
//赋值
self.scrollView = scrollView;
scrollView.delegate = self;
}
//屏幕宽度
#define XFJUIScreenW [UIScreen mainScreen].bounds.size.width
//屏幕高度
#define XFJUIScreenH [UIScreen mainScreen].bounds.size.height
#pragma mark - 添加标题ScrollView
- (void)setUptitle
{
//创建UIScrollView对象
UIScrollView *titleView = [[UIScrollView alloc] init];
//设置颜色
titleView.backgroundColor = [UIColor clearColor];
//设置尺寸
titleView.frame = CGRectMake(0, 64, XFJUIScreenW, 35);
//设置滚动条
titleView.showsHorizontalScrollIndicator = NO;
//添加到控制器view中
[self.view addSubview:titleView];
//赋值
self.titleView = titleView;
}
#pragma mark - 设置控制器
- (void)setUpChildController
{
//热点
XFJHotViewController *hvc = [[XFJHotViewController alloc] init];
//设置标题
hvc.title = @"热点";
[self addChildViewController:hvc];
//订阅
XFJReadViewController *rvc = [[XFJReadViewController alloc] init];
//设置标题
rvc.title = @"订阅";
[self addChildViewController:rvc];
//头条
XFJTopLineViewController *tvc = [[XFJTopLineViewController alloc] init];
//设置标题
tvc.title = @"头条";
[self addChildViewController:tvc];
//科技
XFJScienceViewController *svc = [[XFJScienceViewController alloc] init];
//设置标题
svc.title = @"科技";
[self addChildViewController:svc];
//社会
XFJSocietyViewController *svcr = [[XFJSocietyViewController alloc] init];
//设置标题
svcr.title = @"社会";
[self addChildViewController:svcr];
//视频
XFJVideoViewController *vvc = [[XFJVideoViewController alloc] init];
//设置标题
vvc.title = @"视频";
[self addChildViewController:vvc];
}
/**
* 标题UIScrollView
*/
@property (nonatomic, weak) UIScrollView *titleView;
/**
* 控制器的UIScrollView
*/
@property (nonatomic, weak) UIScrollView *scrollView;
self.automaticallyAdjustsScrollViewInsets = NO;
#pragma mark - 设置标题按钮
- (void)setUpTitleBtn
{
//取出标题中所有的子控件
NSInteger count = self.childViewControllers.count;
//创建的按钮的尺寸
NSInteger btnX = 0;
NSInteger btnY = 0;
NSInteger btnW = 100;
NSInteger btnH = 35;
//遍历所有的子控件
for (NSInteger i = 0; i < count; i++) {
//每次遍历的时候创建一个按钮
UIButton *btn = [[UIButton alloc] init];
//绑定每一个按钮的tag
btn.tag = i;
//x的值随着按钮的宽度变化
btnX = i * btnW;
//设置尺寸
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
//在遍历的时候取出所有的子控制器
UIViewController *vc = self.childViewControllers[i];
//设置按钮的标题
[btn setTitle:vc.title forState:UIControlStateNormal];
//监听按钮的点击事件
[btn addTarget:self action:@selector(setUpBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//设置按钮的文字
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//添加按钮到标题中
[self.titleView addSubview:btn];
/**
* 选中按钮
*/
@property (nonatomic, weak) UIButton *selectBtn;
#pragma mark - 设置选中的按钮状态
- (void)selectBtnClick:(UIButton *)button
{
//恢复上一个按钮的颜色状态
[self.selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//设置当前按钮的状态
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//让当前按钮成为选中状态
self.selectBtn = button;
}
#pragma mark - 创建控制器
- (void)setUpOneController:(NSInteger)i
{
//根据指定的tag,取出对应的控制器
UIViewController *vc = self.childViewControllers[i];
//设置控制器的x值
CGFloat x = i * XFJUIScreenW;
//判断是否含有父控制器,如果有的话
if ([vc.view superview]) {
return;
}
//设置控制器的尺寸
vc.view.frame = CGRectMake(x, 0, XFJUIScreenW, self.scrollView.bounds.size.height);
//添加控制器到view中
[self.scrollView addSubview:vc.view];
}
#pragma mark - 实现监听方法
- (void)setUpBtnClick:(UIButton *)btn
{
//设置按钮颜色状态
[self selectBtnClick:btn];
NSInteger i = btn.tag;
//调用创建控制器的方法
[self setUpOneController:i];
//设置控制器的x值
CGFloat x = i * XFJUIScreenW;
//让显示控制器的view显示指定的位置
self.scrollView.contentOffset = CGPointMake(x, 0);
}
#pragma mark - scrollView将要停止拖动的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//停止滚动角标
NSInteger i = scrollView.contentOffset.x / XFJUIScreenW;
//取出当前的标题号
UIButton *btn = self.titleButtons[i];
//解决了bug-----> btn没有打印出值,是因为定义属性的时候,使用了weak,所有不可能保存按钮.
NSLog(@"%@",btn);
//令当前按钮成选中状态
[self selectBtnClick:btn];
//添加相应的子控制器
[self setUpOneController:i];
}
//判断让按钮默认第一个为选中状态
if (i == 0) {
[self setUpBtnClick:btn];
}
//将按钮加入到数组中
[self.titleButtons addObject:btn];
}
?//设置滚动范围
CGFloat scope = count * btnW;
//设置滑动的contentSize
self.titleView.contentSize = CGSizeMake(scope, 0);
//开启分页功能
self.scrollView.pagingEnabled = YES;
//隐藏底部的滑动条
self.scrollView.showsHorizontalScrollIndicator = NO;
//设置控制器的scrollView的滚动范围
self.scrollView.contentSize = CGSizeMake(count * XFJUIScreenW, 0);
#pragma mark - 设置选中的按钮状态
- (void)selectBtnClick:(UIButton *)button
{
//恢复上一个按钮的颜色状态
[self.selectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//取消上一个字体形变
self.selectBtn.transform = CGAffineTransformIdentity;
//设置当前按钮的状态
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//设置随着控制器的view而滚动的titleView
//计算滚动区域
CGFloat offsetX = button.center.x - XFJUIScreenW * 0.5;
//判断左边最大的滚动范围
if (offsetX < 0) {
offsetX = 0;
}
//计算右边最大的滚动范围
CGFloat offmax = self.titleView.contentSize.width - XFJUIScreenW;
//判断如果大于最大的偏移量,那么就让其等于最大的偏移量
if (offsetX > offmax) {
offsetX = offmax;
}
//动画效果
[self.titleView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
//方法字体(选中状态)
button.transform = CGAffineTransformMakeScale(1.3, 1.3);
//让当前按钮成为选中状态
self.selectBtn = button;
}
#pragma mark - 设置滑动的时候字体的放缩(代理方法,滚动标题就会缩放)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//获取当前按钮的角标--->通过打印屏幕的角标
NSInteger leftI = scrollView.contentOffset.x / XFJUIScreenW;
//根据屏幕的角标去按钮的数组里面找对应的按钮
UIButton *leftBtn = self.titleButtons[leftI];
//获取右边的按钮
NSInteger rightI = leftI + 1;
UIButton *rightButton;
//判断右边按钮的范围,不能超过数组的总数,否则会数组越界
if (rightI < self.titleButtons.count) {
//获取右边的按钮
rightButton = self.titleButtons[rightI];
}
//右边按钮的缩放量
CGFloat rightScale = scrollView.contentOffset.x / XFJUIScreenW - leftI;
//左边按钮的缩放量
CGFloat leftScale = 1 - rightScale;
//对标题进行缩放
rightButton.transform = CGAffineTransformMakeScale(rightScale * 0.3 + 1,rightScale * 0.3 + 1 );
leftBtn.transform = CGAffineTransformMakeScale(leftScale * 0.3 + 1, leftScale * 0.3 + 1);
//滑动的时候,字体颜色的渐变(颜色指示器R;G;B)
UIColor *rightColor = [UIColor colorWithRed:rightScale green:0 blue:0 alpha:1];
[rightButton setTitleColor:rightColor forState:UIControlStateNormal];
UIColor *leftColor = [UIColor colorWithRed:leftScale green:0 blue:0 alpha:1];
[leftBtn setTitleColor:leftColor forState:UIControlStateNormal];
}
#pragma mark - 在view即将显示的时候添加标题,保证viewDidLoad的中的代码都执行完了.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//如果没有添加标题,就添加
if (self.isInitial == NO) {
[self setUpTitleBtn];
//已经添加了标题
self.isInitial = YES;
}
}
原文:http://blog.csdn.net/xf931456371/article/details/51009799