#import "ViewController.h"
#import "LyTableViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
{
UITableView *_tableView;
NSMutableArray *_vcArray;
NSMutableArray *_btnArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configUI];
}
#pragma mark - configUI
- (void)configUI {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40)];
_btnArray = [NSMutableArray array];
NSArray *titles = @[@"头条", @"娱乐", @"热点", @"体育"];
for (NSInteger i = 0; i < titles.count; i ++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i * self.view.frame.size.width / titles.count, 0, self.view.frame.size.width / titles.count, 40);
[btn setTitle:titles[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0] forState:UIControlStateNormal];
if (i == 0) {
[btn setTitleColor:[UIColor colorWithRed:0.8 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
}
btn.tag = 1000 + i;
btn.titleLabel.font = [UIFont systemFontOfSize:15];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btn];
[_btnArray addObject:btn];
}
[self.view addSubview:view];
_vcArray = [NSMutableArray array];
for (NSInteger i = 0; i < _btnArray.count; i ++) {
LyTableViewController *lytabVc = [[LyTableViewController alloc] init];
lytabVc.index = i;
lytabVc.stringstring = titles[i];
[_vcArray addObject:lytabVc];
[self addChildViewController:lytabVc];
}
self.automaticallyAdjustsScrollViewInsets = NO;
_tableView = [[UITableView alloc] init];
_tableView.pagingEnabled = YES;
_tableView.bounces = NO;
// 去除分割线
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.transform = CGAffineTransformMakeRotation(-M_PI_2);
_tableView.frame = CGRectMake(0, 64 + 40, self.view.frame.size.width, self.view.frame.size.height - 64 - 40);
_tableView.showsVerticalScrollIndicator = NO;
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
[self.view addSubview:_tableView];
}
- (void)btnClick:(UIButton *)btn {
NSLog(@"haha");
// 第btnNum个btn
// NSInteger btnNum = btn.tag - 1000;
}
#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _vcArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.transform = CGAffineTransformMakeRotation(M_PI_2);
// cell.textLabel.text = [NSString stringWithFormat:@"%ld, %ld", (long)indexPath.section, (long)indexPath.row];
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
LyTableViewController *lyTabVc = _vcArray[indexPath.row];
lyTabVc.view.frame = cell.contentView.bounds;
[cell.contentView addSubview:lyTabVc.view];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return self.view.frame.size.width;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// tableView偏移量
CGFloat horizontalOffset = scrollView.contentOffset.y;
// 屏幕宽度
CGFloat screenWidth = self.view.frame.size.width;
// 比例
CGFloat offsetRatio = (NSInteger)horizontalOffset % (NSInteger)screenWidth / screenWidth;
// 当前页
NSInteger curIndex = (horizontalOffset + screenWidth / 2) / screenWidth;
// 过滤滚动结束
if (curIndex * screenWidth != horizontalOffset) {
NSInteger animateIndex = horizontalOffset > curIndex * screenWidth ? curIndex + 1 : curIndex - 1;
if (curIndex > animateIndex) {
offsetRatio = 1 - offsetRatio;
}
UIButton *curBtn = _btnArray[curIndex];
UIButton *animateBtn = _btnArray[animateIndex];
[curBtn setTitleColor:[UIColor colorWithRed:0.8 green:0.8 * offsetRatio blue:0.8 * offsetRatio alpha:1.0] forState:UIControlStateNormal];
[animateBtn setTitleColor:[UIColor colorWithRed:0.8 green:0.8 * (1 - offsetRatio) blue:0.8 * (1 - offsetRatio) alpha:1.0] forState:UIControlStateNormal];
}
}
@end
HorizontalTableView
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 说明: 最近在测试项目里的一个水平表格时发现一个问题:模仿UITableView实现的HorizontalTabl...
- Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
- 一直以来,晚睡让我养成了很多坏习惯,熬夜导致早上起不来,会赖床,习惯赶忙,上班掐点,迟到,来不及吃早餐.........