美文网首页
HorizontalTableView

HorizontalTableView

作者: March_Cullen | 来源:发表于2017-03-02 21:19 被阅读0次
    #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

          本文链接:https://www.haomeiwen.com/subject/tvikgttx.html