XMGEssenceViewController
精華頁面里有5個子控制器(全部,視頻治笨,聲音驳概,圖片粪小,段子)這5個控制器都是XMGTopicViewController,只不過其中顯示的cell的樣式是不一樣的抡句。并且這5個控制器是可以通過手勢來進(jìn)行切換的探膊,或者是通過上邊的(全部,視頻待榔,聲音逞壁,圖片,段子)的按鈕進(jìn)行點擊切換
@interface XMGEssenceViewController() <UIScrollViewDelegate>
/** 標(biāo)簽欄底部的紅色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 當(dāng)前選中的按鈕 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 頂部的所有標(biāo)簽 */
@property (nonatomic, weak) UIView *titlesView;
/** 底部的所有內(nèi)容 */
@property (nonatomic, weak) UIScrollView *contentView;
@end
-(void)viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
// 設(shè)置導(dǎo)航欄
[self setupNav];
// 初始化子控制器
[self setupChildVces];
// 設(shè)置頂部的標(biāo)簽欄
[self setupTitlesView];
// 底部的scrollView
[self setupContentView];
}
初始化子控制器锐锣,就是那5個子控制器腌闯,
- (void)setupChildVces
{
XMGTopicViewController *all = [[XMGTopicViewController alloc] init];
all.title = @"全部";
all.type = XMGTopicTypeAll;
[self addChildViewController:all];
XMGTopicViewController *video = [[XMGTopicViewController alloc] init];
video.title = @"視頻";
video.type = XMGTopicTypeVideo;
[self addChildViewController:video];
XMGTopicViewController *voice = [[XMGTopicViewController alloc] init];
voice.title = @"聲音";
voice.type = XMGTopicTypeVoice;
[self addChildViewController:voice];
XMGTopicViewController *picture = [[XMGTopicViewController alloc] init];
picture.title = @"圖片";
picture.type = XMGTopicTypePicture;
[self addChildViewController:picture];
XMGTopicViewController *word = [[XMGTopicViewController alloc] init];
word.title = @"段子";
word.type = XMGTopicTypeWord;
[self addChildViewController:word];
}
設(shè)置頂部的標(biāo)簽欄
- (void)setupTitlesView
{
// 標(biāo)簽欄整體
UIView *titlesView = [[UIView alloc] init];
titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
titlesView.width = self.view.width;
titlesView.height = XMGTitilesViewH;
titlesView.y =XMGTitilesViewY;
[self.view addSubview:titlesView];
self.titlesView = titlesView;
// 底部的紅色指示器
UIView *indicatorView = [[UIView alloc] init];
indicatorView.backgroundColor = [UIColor redColor];
indicatorView.height = 2;
indicatorView.tag = -1;
indicatorView.y = titlesView.height - indicatorView.height;
self.indicatorView = indicatorView;
// 內(nèi)部的子標(biāo)簽
CGFloat width = titlesView.width / self.childViewControllers.count;
CGFloat height = titlesView.height;
for (NSInteger i = 0; i<self.childViewControllers.count; i++) {
UIButton *button = [[UIButton alloc] init];
button.tag = i;
button.height = height;
button.width = width;
button.x = i * width;
UIViewController *vc = self.childViewControllers[i];
[button setTitle:vc.title forState:UIControlStateNormal];
//[button layoutIfNeeded]; // 強(qiáng)制布局(強(qiáng)制更新子控件的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];
// 默認(rèn)點擊了第一個按鈕
if (i == 0) {
button.enabled = NO;
self.selectedButton = button;
// 讓按鈕內(nèi)部的label根據(jù)文字內(nèi)容來計算尺寸
[button.titleLabel sizeToFit];
self.indicatorView.width = button.titleLabel.width;
self.indicatorView.centerX = button.centerX;
}
}
[titlesView addSubview:indicatorView];
}
-(void)titleClick:(UIButton *)button{}
- (void)titleClick:(UIButton *)button
{
// 修改按鈕狀態(tài)
self.selectedButton.enabled = YES;
button.enabled = NO;
self.selectedButton = button;
// 動畫
[UIView animateWithDuration:0.25 animations:^{
self.indicatorView.width = button.titleLabel.width;
self.indicatorView.centerX = button.centerX;
}];
// 滾動
CGPoint offset = self.contentView.contentOffset;
offset.x = button.tag * self.contentView.width;
[self.contentView setContentOffset:offset animated:YES];
}
底部的scrollView
- (void)setupContentView
{
// 不要自動調(diào)整inset
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView *contentView = [[UIScrollView alloc] init];
contentView.frame = self.view.bounds;
contentView.delegate = self;
contentView.pagingEnabled = YES;
[self.view insertSubview:contentView atIndex:0];
contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
self.contentView = contentView;
// 添加第一個控制器的view
[self scrollViewDidEndScrollingAnimation:contentView];
}
設(shè)置導(dǎo)航欄
- (void)setupNav
{
// 設(shè)置導(dǎo)航欄標(biāo)題
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
// 設(shè)置導(dǎo)航欄左邊的按鈕
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(tagClick)];
// 設(shè)置背景色
self.view.backgroundColor = XMGGlobalBg;
}
-(void)tagClick
- (void)tagClick
{
XMGRecommendTagsViewController *tags = [[XMGRecommendTagsViewController alloc] init];
[self.navigationController pushViewController:tags animated:YES];
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
// 當(dāng)前的索引
NSInteger index = scrollView.contentOffset.x / scrollView.width;
// 取出子控制器
UIViewController *vc = self.childViewControllers[index];
vc.view.x = scrollView.contentOffset.x;
vc.view.y = 0; // 設(shè)置控制器view的y值為0(默認(rèn)是20)
vc.view.height = scrollView.height; // 設(shè)置控制器view的height值為整個屏幕的高度(默認(rèn)是比屏幕高度少個20)
[scrollView addSubview:vc.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self scrollViewDidEndScrollingAnimation:scrollView];
// 點擊按鈕
NSInteger index = scrollView.contentOffset.x / scrollView.width;
[self titleClick:self.titlesView.subviews[index]];
}