基于scrollView的菜单标题对应tableview的段头标题

一个scrollView联动tableview的组件,网上基本都是横向滑动,因项目需要写了竖向滑动组件,记录一下。组件主要功能实现了基于scrollView的菜单标题对应tableview的段头标题。

(低仿支付宝点击首页“更多”跳转页的其中一个组件)


2018-01-05 17_29_12.gif
以下为所有实现代码
#import "ViewController.h"
#import "UIView+XMGExtension.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define KscreenHight [UIScreen mainScreen].bounds.size.height
static CGFloat topH = 150.0f;
static CGFloat menuH = 37.0f;
static NSString *const cellIdentifier = @"cellIdentifier";

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

/** 顶部滑动视图背景 */
@property (nonatomic,strong)UIScrollView *cateListScrollView;
/** 数据源 */
@property (nonatomic,copy)NSArray *dataArr;
/** 列表 */
@property (nonatomic, strong) UITableView *tableView;
/** 头部视图 */
@property (nonatomic, strong) UIView *headView;
/** 当前选中按钮 */
@property (nonatomic, strong) UIButton *currentBut;
/**红色指示器*/
@property (weak, nonatomic) UIView *iundicatyorView;

@property (nonatomic, strong) NSMutableArray *items;
@end

@implementation ViewController

#pragma mark - lazy
- (NSMutableArray *)items
{
    if (_items == nil) {
        _items = [NSMutableArray array];
    }
    return _items;
}

- (UIView *)headView
{
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, topH)];
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.image = [UIImage imageNamed:@"bg_img"];
        imgView.frame = _headView.frame;
        [_headView addSubview:imgView];
        [self.view addSubview:_headView];
    }
    return _headView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topH + menuH, kScreenWidth, KscreenHight - (topH + menuH)) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark - 系统回调
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self headView];
    [self tableView];
    NSArray *arr = @[@"嘿嘿", @"哦你空间", @"额网管", @"欧力银行卡", @"潘", @"大发发发的", @"屁也很高", @"气味儿", @"平均分"];
    self.dataArr = arr;
    [self createsUpScrollView:arr];
}

#pragma mark - private methods
/** 设置顶部标签栏 */
- (void)createsUpScrollView:(NSArray *)array
{
    // 顶部滑动视图
    self.cateListScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, topH, kScreenWidth, menuH)];
    self.cateListScrollView.backgroundColor = [UIColor yellowColor];
    self.cateListScrollView.tag = 3000;
    self.cateListScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.cateListScrollView];

    CGFloat space = 6;
    CGFloat widthContentSize = 0;
    UIColor *textLineColor = [UIColor redColor];
    for (NSInteger i = 0; i < array.count; i++) {
        NSString *pageName = self.dataArr[i];
        NSInteger lenght = [pageName length];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(space + widthContentSize, 3, 14*lenght + space , 31);
        btn.tag = i;
        [btn setTitle:pageName forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 确保选中的时候不能被点击而重复请求
        [btn setTitleColor:textLineColor forState:UIControlStateDisabled];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.cateListScrollView addSubview:btn];
        [self.items addObject:btn];
        // 如果是第一个按钮
        if (i == 0) {
            btn.enabled = NO;
            self.currentBut = btn;
            [btn.titleLabel sizeToFit];
        }
        widthContentSize = 14 * lenght + space + space + widthContentSize;
        [btn addTarget:self action:@selector(selectIndexTableViewAndCollectionView:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    // 指示器(此处有点傻,如果不设置初始坐标,初次不会显示指示器)
    UIView *bottonView = [[UIView alloc]init];
    bottonView.xmg_height = 2;
    bottonView.xmg_x = 8;
    bottonView.xmg_width = 28;
    bottonView.tag = -12; // 区分按钮
    bottonView.xmg_y = menuH - 2;
    bottonView.backgroundColor = textLineColor;
    [self.cateListScrollView addSubview:bottonView];
    self.iundicatyorView = bottonView;
    // 设置滑动范围
    self.cateListScrollView.contentSize = CGSizeMake(widthContentSize + space, menuH);
}

#pragma mark - incident response
/** 点击标题按钮 */
- (void)selectIndexTableViewAndCollectionView:(UIButton *)button
{
    self.currentBut.enabled = YES;
    button.enabled = NO;
    self.currentBut = button;
    // 指示器跟随滑动显示
    [UIView animateWithDuration:0.25 animations:^{
        self.iundicatyorView.xmg_width = self.currentBut.titleLabel.xmg_width;
        self.iundicatyorView.xmg_centerX = self.currentBut.xmg_centerX;
    }];
    // 根据下标滑动tableView对应位置
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:button.tag] animated:YES scrollPosition:UITableViewScrollPositionTop];
}


#pragma mark - UITableView Delegate/Datasource
// 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.textLabel.text = @"👌😁🎲";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 37.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1.0f;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.dataArr[section];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 修改位置
    CGFloat offSetY = scrollView.contentOffset.y;
    if (offSetY > 50.0f) {
        [UIView animateWithDuration:0.2 animations:^{
            self.cateListScrollView.xmg_y = 20;
            self.tableView.xmg_y = 20 + menuH;
            self.tableView.xmg_height = KscreenHight - menuH - 20;
        }];
        
    }else {
        [UIView animateWithDuration:0.25 animations:^{
            self.cateListScrollView.xmg_y = topH;
            self.tableView.xmg_y = topH + menuH;
            self.tableView.xmg_height = KscreenHight - menuH - topH;
        }];
    }
}

// 将要显示新的cell的时候调用此方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 如果不是用户滚动的,就不要往下走了
    if (!(tableView.isDragging || tableView.isDecelerating || tableView.isTracking)) {
        return;
    }
    // 取出按钮
    UIButton *button = self.items[indexPath.section];
    self.currentBut.enabled = YES;
    button.enabled = NO;
    self.currentBut = button;
    // 指示器跟随滑动
    [UIView animateWithDuration:0.25 animations:^{
        self.iundicatyorView.xmg_width = self.currentBut.titleLabel.xmg_width;
        self.iundicatyorView.xmg_centerX = self.currentBut.xmg_centerX;
    }];
}

// 段头即将出现
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    UIButton *button = self.items[section];
    CGFloat buttonW = button.frame.origin.x + button.frame.size.width;
    if (buttonW > kScreenWidth) {
        [UIView animateWithDuration:0.25 animations:^{
            self.cateListScrollView.contentOffset = CGPointMake(buttonW - kScreenWidth, 0);
        }];
    }else {
        self.cateListScrollView.contentOffset = CGPointMake(0, 0);
    }
}

@end
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,109评论 4 62
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,769评论 22 665
  • 原文链接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影阅读 32,942评论 6 472
  • 不知不觉中,人生已近半。 仿佛弹指一挥间, 我们的生命又画上了一道年轮。 有怎样的年龄,就有怎样的人生使命; ...
    今生为谁阅读 372评论 0 4
  • 对自己好点 不要自责 要努力才有选择 鸡汤喝多了就有了幻想 过好当下 忘记不该记着的人 不要在触景生情 忘记一件事...
    檒缘阅读 93评论 0 0