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的方...