效果如下
#import"XMGEssenceViewController.h"
#import"XMGRecommendTagsViewController.h"
#import"XMGVideoViewController.h"
#import"XMGVoiceViewController.h"
#import"XMGAllViewController.h"
#import"XMGPictureViewController.h"
#import"XMGWordViewController.h"
@interfaceXMGEssenceViewController()
/**标签栏底部的红色指示器*/
@property(nonatomic,strong)UIView*indicatorView;
/**当前选中的按钮*/
@property(nonatomic,weak)UIButton*selectButton;
/**顶部所有的标签*/
@property(weak,nonatomic)UIView*titlesView;
/**顶部所有的内容*/
@property(weak,nonatomic)UIScrollView*contentView;
@end
@implementationXMGEssenceViewController
- (void)viewDidLoad {
[superviewDidLoad];
//设置导航栏
[selfsetupNav];
//初始化子控制器
[selfsetupChildVces];
//设置顶部标签
[selfsetupTitlesView];
//底部的scrollView
[selfsetupContentView];
}
/**初始化子控制器*/
-(void)setupChildVces{
XMGAllViewController*all=[[XMGAllViewControlleralloc]init];
[selfaddChildViewController:all];
XMGVideoViewController*video=[[XMGVideoViewControlleralloc]init];
[selfaddChildViewController:video];
XMGVoiceViewController*voice=[[XMGVoiceViewControlleralloc]init];
[selfaddChildViewController:voice];
XMGPictureViewController*picture=[[XMGPictureViewControlleralloc]init];
[selfaddChildViewController:picture];
XMGWordViewController*word=[[XMGWordViewControlleralloc]init];
[selfaddChildViewController:word];
}
/**设置底部的setupScrollView */
-(void)setupContentView{
//不要自动调整inset
self.automaticallyAdjustsScrollViewInsets=NO;
UIScrollView*contentView=[[UIScrollViewalloc]init];
contentView.frame=self.view.bounds;
//设置内边距
// CGFloat bottom = self.tabBarController.tabBar.height;
// CGFloat top = CGRectGetMaxY(self.titlesView.frame);
// contentView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
contentView.delegate=self;
//设置分页
contentView.pagingEnabled=YES;
[self.viewinsertSubview:contentViewatIndex:0];
contentView.contentSize=CGSizeMake(contentView.width*self.childViewControllers.count,0);
self.contentView=contentView;
//添加第一个控制器View
[selfscrollViewDidEndScrollingAnimation:contentView];
}
/**设置顶部标签*/
-(void)setupTitlesView{
UIView*titlesView=[[UIViewalloc]init];
//设置颜色半透明
// titlesView.backgroundColor=[UIColor colorWithRed:1.0 green:1.00 blue:1.0 alpha:0.5];
// titlesView.backgroundColor=[UIColor colorWithWhite:1.0 alpha:0.5];
titlesView.backgroundColor=[[UIColorwhiteColor]colorWithAlphaComponent:0.5];
titlesView.width=self.view.width;
titlesView.height=35;
titlesView.y=64;
[self.viewaddSubview:titlesView];
self.titlesView=titlesView;
//底部红色指示器
UIView*indicatorView=[[UIViewalloc]init];
indicatorView.backgroundColor= [UIColorredColor];
indicatorView.height=2;
indicatorView.tag= -1;
indicatorView.y=titlesView.height-indicatorView.height;
self.indicatorView= indicatorView;
//内部的子标签
NSArray*titles=@[@"全部",@"视频",@"声音",@"图片",@"段子"];
CGFloatwidth=titlesView.width/titles.count;
CGFloatheight=titlesView.height;
for(NSIntegeri=0; i
UIButton*button=[[UIButtonalloc]init];
button.tag=i;
button.height=height;
button.width=width;
button.x=i*width;
[buttonsetTitle:titles[i]forState:UIControlStateNormal];
// [button layoutIfNeeded];//强制布局(强制更新子控件的frame)
[buttonsetTitleColor:[UIColorgrayColor]forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateDisabled];
button.titleLabel.font=[UIFontsystemFontOfSize:14];
[buttonaddTarget:selfaction:@selector(titleClick:)forControlEvents:UIControlEventTouchUpInside];
[titlesViewaddSubview:button];
//默认点击了第一个按钮
if(i ==0) {
button.enabled=NO;
self.selectButton= button;
//方法一让按钮内部的label根据文字内容来计算尺寸
// [button.titleLabel sizeToFit];
// self.indicatorView.width=button.titleLabel.width;
//方法二根据数组文字的多少,算出器titleLabel的宽度
self.indicatorView.width=[titles[i]sizeWithAttributes:@{NSFontAttributeName:button.titleLabel.font}].width;
self.indicatorView.centerX= button.centerX;
}
}
[titlesViewaddSubview:self.indicatorView];
}
/**顶部标签按钮的点击事件*/
-(void)titleClick:(UIButton*)button{
XMGLogFunc;
//修改按钮状态
self.selectButton.enabled=YES;
button.enabled=NO;
self.selectButton= button;
//动画
[UIViewanimateWithDuration:0.25animations:^{
self.indicatorView.width=button.titleLabel.width;
self.indicatorView.centerX=button.centerX;
}];
//滚动
CGPointoffset=self.contentView.contentOffset;
offset.x=button.tag*self.contentView.width;
[self.contentViewsetContentOffset:offsetanimated:YES];
}
/**设置导航栏*/
-(void)setupNav{
//设置导航栏内容
self.navigationItem.titleView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"MainTitle"]];
self.navigationItem.leftBarButtonItem=[UIBarButtonItemitemWithImage:@"MainTagSubIcon"highImage:@"MainTagSubIconClick"target:selfaction:@selector(tagClick)];
//设置背景色
self.view.backgroundColor=XMGGlobalBg;
}
-(void)tagClick{
XMGRecommendTagsViewController*tags=[[XMGRecommendTagsViewControlleralloc]init];
[self.navigationControllerpushViewController:tagsanimated:YES];
}
#pragma mark - 当scrollView动画结束后,在加载控制器
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView*)scrollView{
//添加子控制器的view
//当前的索引
NSIntegerindex = scrollView.contentOffset.x/scrollView.width;
//取出子控制器
UITableViewController*vc =self.childViewControllers[index];
vc.view.x= scrollView.contentOffset.x;
vc.view.y=0;//设置控制器view的y值为0默认是20
vc.view.height= scrollView.height;//设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少了20)
//设置内边距
CGFloatbottom =self.tabBarController.tabBar.height;
CGFloattop =CGRectGetMaxY(self.titlesView.frame);
vc.tableView.contentInset=UIEdgeInsetsMake(top,0, bottom,0);
//设置滚动条的内边距
vc.tableView.scrollIndicatorInsets= vc.tableView.contentInset;
[scrollViewaddSubview:vc.view];
}
#pragma mark - 停止检测
-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{
[selfscrollViewDidEndScrollingAnimation:scrollView];
//点击按钮
NSIntegerindex = scrollView.contentOffset.x/scrollView.width;
[selftitleClick:self.titlesView.subviews[index]];
}
@end