import "LYEssenceViewController.h"
import "LYRecommendTagsViewController.h"
import "LYAllViewController.h"
import "LYVideoViewController.h"
import "LYVoiceViewController.h"
import "LYPictureViewController.h"
import "LYWordViewController.h"
@interface LYEssenceViewController ()<UIScrollViewDelegate>
{
UIButton *_selecteon;
UIView *_indicatorView;
NSArray *_titles;
UIScrollView *_scrollView;
UIView *_titlesView;
}
@end
@implementation LYEssenceViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = LYGlobalRGB;
self.automaticallyAdjustsScrollViewInsets = NO;
[self setUpNav];
// 初始化子控制器
[self setUpChildVcs];
[self setUpTitlesView];
[self setUpScrollView];
}
- (void)setUpChildVcs {
LYAllViewController *all = [[LYAllViewController alloc] init];
[self addChildViewController:all];
LYVideoViewController *video = [[LYVideoViewController alloc] init];
[self addChildViewController:video];
LYVoiceViewController *voice = [[LYVoiceViewController alloc] init];
[self addChildViewController:voice];
LYPictureViewController *picture = [[LYPictureViewController alloc] init];
[self addChildViewController:picture];
LYWordViewController *word = [[LYWordViewController alloc] init];
[self addChildViewController:word];
}
- (void)setUpNav {
self.navigationItem.title = @"百思不得姐";
// self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top_navigation_middle_night"]];
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"top_games_btn" highLightedImage:@"top_games_btn_pressed" target:self action:@selector(tagsBtnClick:)];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"top_suiji_btn_2" highLightedImage:@"top_suiji_btn_pressed_2" target:self action:@selector(suiJiBtnClick:)];
}
- (void)tagsBtnClick:(UIButton *)btn {
LYLogFunc;
LYRecommendTagsViewController *tags = [[LYRecommendTagsViewController alloc] init];
[self.navigationController pushViewController:tags animated:YES];
}
- (void)suiJiBtnClick:(UIButton *)btn {
LYLogFunc;
}
- (void)setUpTitlesView {
// 标签栏整体
_titlesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 35)];
_titlesView.backgroundColor = LYRGBAColor(1.0, 1.0, 1.0, 0.1);
// titlesView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
// titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
[self.view addSubview:_titlesView];
// 内部子标签
_titles = @[@"全部", @"视频", @"声音", @"图片", @"段子"];
for (NSInteger i = 0; i < _titles.count; i ++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(i * _titlesView.frame.size.width / _titles.count, 0, _titlesView.frame.size.width / _titles.count, _titlesView.frame.size.height);
if (i == 0) {
button.enabled = NO;
_selecteon = button;
[button.titleLabel sizeToFit];
}
[button setTitle:_titles[i] forState:UIControlStateNormal];
[button layoutIfNeeded]; // 强制布局(强制更新子控件的frame)
button.titleLabel.font = [UIFont systemFontOfSize:14.0];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 100 + i;
[_titlesView addSubview:button];
}
_indicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 40 - 2, _titlesView.frame.size.width / _titles.count, 2)];
_indicatorView.backgroundColor = [UIColor redColor];
_indicatorView.tag = 10;
[_titlesView addSubview:_indicatorView];
}
- (void)buttonClick:(UIButton *)button {
_selecteon.enabled = YES;
button.enabled = NO;
_selecteon = button;
[UIView animateWithDuration:0.5 animations:^{
_indicatorView.frame = CGRectMake(button.frame.origin.x, 40 - 2, self.view.frame.size.width / _titles.count, _indicatorView.frame.size.height);
}];
// 滚动
CGPoint offset = _scrollView.contentOffset;
offset.x = (button.tag - 100) * _scrollView.frame.size.width;
[_scrollView setContentOffset:offset animated:YES];
// 下面的方法不能实现_scrollView的滚动,应采用上面的方法
// _scrollView.contentOffset = CGPointMake((button.tag - 100) * _scrollView.frame.size.width, 0);
}
- (void)setUpScrollView {
self.automaticallyAdjustsScrollViewInsets = NO;
_scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.contentSize = CGSizeMake(_scrollView.width * self.childViewControllers.count, 0);
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
[self.view insertSubview:_scrollView atIndex:0];
// 添加第一个控制器的view
[self scrollViewDidEndScrollingAnimation:_scrollView];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
// 添加子控制器的view
// 当前的索引
NSInteger index = scrollView.contentOffset.x / scrollView.width;
UITableViewController *tbVc = self.childViewControllers[index];
tbVc.view.x = scrollView.contentOffset.x;
// 设置内边距
CGFloat top = CGRectGetMaxY(_titlesView.frame);
CGFloat bottom = self.tabBarController.tabBar.height;
tbVc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
// tbVc.tableView.contentInset = UIEdgeInsetsMake(0, 20, 49 + 64, 0);
[scrollView addSubview:tbVc.view];
// [scrollView addSubview:tbVc.tableView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self scrollViewDidEndScrollingAnimation:scrollView];
NSInteger index = scrollView.contentOffset.x / scrollView.width;
[self buttonClick:_titlesView.subviews[index]];
// [self buttonClick:(UIButton *)[self.view viewWithTag:scrollView.contentOffset.x / scrollView.width]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
scrollView的滚动
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Webview与ScrollView的滚动兼容及留白处理 背景 开发中我们经常会遇到使用网页来显示图文内容,而且往...
- 要实现ScrollView滚动条的隐藏,有两种方法,一种是在XML的ScrollView布局中加入属性androi...
- android判断scrollview滚动到底和滚动到右的问题 第一步,获取子view的宽度scrollview有...
- ScrollView 滚动监听,一般不能直接使用,需要重写ScrollView的onScrollChanged的方...