精华页面基本框架
精华.gif
该页面中间部分为一个view加一个scrollView.view上面添加了五个按钮,点击按钮时,下面的scrollView跟随着滚动.当滚动下面的scrollView的时候,上面的按钮也跟随着变化相应的模式.
/**
添加子控制器
*/
- (void)setupChildVc {
LXXAllViewController *allVc = [[LXXAllViewController alloc] init];
[self addChildViewController:allVc];
LXXVedioViewController *vedioVc = [[LXXVedioViewController alloc] init];
[self addChildViewController:vedioVc];
LXXVoiceViewController *voiceVc = [[LXXVoiceViewController alloc] init];
[self addChildViewController:voiceVc];
LXXPictureViewController *picVc = [[LXXPictureViewController alloc] init];
[self addChildViewController:picVc];
LXXWordViewController *wordVc = [[LXXWordViewController alloc] init];
[self addChildViewController:wordVc];
}
/**
设置导航栏
*/
- (void)setupNav {
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
//设置左上角
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(mainTagSubClick)];
self.view.backgroundColor = LXXCommonBgColor;
}
/**
添加titleView
*/
- (void)setupTitleView {
//创建titleView
CGFloat titleViewW = KScreenW;
CGFloat titleViewH = 35;
CGFloat titleViewY = 64;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, titleViewY, titleViewW, titleViewH)];
titleView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
self.titleView = titleView;
[self.view addSubview:titleView];
//创建底部显示view
CGFloat indicatorViewH = 2;
UIView *indicatorView = [[UIView alloc] init];
indicatorView.y = titleViewH - indicatorViewH;
indicatorView.x = 0;
indicatorView.tag = -1;
indicatorView.height = indicatorViewH;
indicatorView.backgroundColor = [UIColor redColor];
self.indicatorView = indicatorView;
//创建title按钮
NSArray *titleArr = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
CGFloat titleBtnY = 0;
CGFloat titleBtnW = KScreenW / titleArr.count;
CGFloat titleBtnH = titleViewH;
for (int i = 0; i < titleArr.count; i++) {
CGFloat titleBtnX = i * titleBtnW;
UIButton *titleBtn = [[UIButton alloc] initWithFrame:CGRectMake(titleBtnX, titleBtnY, titleBtnW, titleBtnH)];
titleBtn.tag = i;
titleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
[titleBtn setTitle:titleArr[i] forState:UIControlStateNormal];
[titleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[titleBtn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
[titleBtn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:titleBtn];
if (i == 0) {//加载时默认选中第一个按钮
titleBtn.enabled = NO;
self.selectedBtn = titleBtn;
[titleBtn.titleLabel sizeToFit];
self.indicatorView.width = titleBtn.titleLabel.width;
self.indicatorView.centerX = titleBtn.centerX;
}
}
[titleView addSubview:indicatorView];
}
/**
监听title按钮的点击
@param btn 点击的按钮
*/
- (void)titleBtnClick:(UIButton *)btn {
self.selectedBtn.enabled = YES;
btn.enabled = NO;
self.selectedBtn = btn;
[UIView animateWithDuration:0.25 animations:^{
self.indicatorView.width = btn.titleLabel.width;
self.indicatorView.centerX = btn.centerX;
}];
// 中间内容滚动
CGPoint offset = self.contentView.contentOffset;
offset.x = btn.tag * self.contentView.width;
[self.contentView setContentOffset:offset animated:YES];
}
/**
设置中间的内容的view
*/
- (void)setupContentView {
// 不要自动调整inset
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView *contentView = [[UIScrollView alloc] init];
contentView.frame = self.view.bounds;
contentView.delegate = self;
contentView.pagingEnabled = YES;
contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
self.contentView = contentView;
[self.view insertSubview:contentView atIndex:0];
// 添加第一个控制器的view
[self scrollViewDidEndScrollingAnimation:contentView];
}
#pragma mark - scrollView代理方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
// 当前的索引
NSInteger index = scrollView.contentOffset.x / scrollView.width;
// 取出子控制器
UITableViewController *vc = self.childViewControllers[index];
vc.view.x = scrollView.contentOffset.x;
vc.view.y = 0;
vc.view.height = scrollView.height;
// 设置内边距
CGFloat bottom = self.tabBarController.tabBar.height;
CGFloat top = CGRectGetMaxY(self.titleView.frame);
vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
// 设置滚动条的内边距
vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
[scrollView addSubview:vc.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self scrollViewDidEndScrollingAnimation:scrollView];
// 点击按钮
NSInteger index = scrollView.contentOffset.x / scrollView.width;
[self titleBtnClick:self.titleView.subviews[index]];
}