基于scrollView的菜單標(biāo)題對應(yīng)tableview的段頭標(biāo)題

一個scrollView聯(lián)動tableview的組件,網(wǎng)上基本都是橫向滑動,因項目需要寫了豎向滑動組件,記錄一下。組件主要功能實現(xiàn)了基于scrollView的菜單標(biāo)題對應(yīng)tableview的段頭標(biāo)題。

(低仿支付寶點擊首頁“更多”跳轉(zhuǎn)頁的其中一個組件)


2018-01-05 17_29_12.gif
以下為所有實現(xiàn)代碼
#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;
/** 數(shù)據(jù)源 */
@property (nonatomic,copy)NSArray *dataArr;
/** 列表 */
@property (nonatomic, strong) UITableView *tableView;
/** 頭部視圖 */
@property (nonatomic, strong) UIView *headView;
/** 當(dāng)前選中按鈕 */
@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 - 系統(tǒng)回調(diào)
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self headView];
    [self tableView];
    NSArray *arr = @[@"嘿嘿", @"哦你空間", @"額網(wǎng)管", @"歐力銀行卡", @"潘", @"大發(fā)發(fā)發(fā)的", @"屁也很高", @"氣味兒", @"平均分"];
    self.dataArr = arr;
    [self createsUpScrollView:arr];
}

#pragma mark - private methods
/** 設(shè)置頂部標(biāo)簽欄 */
- (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];
        // 確保選中的時候不能被點擊而重復(fù)請求
        [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];
    }
    
    // 指示器(此處有點傻,如果不設(shè)置初始坐標(biāo),初次不會顯示指示器)
    UIView *bottonView = [[UIView alloc]init];
    bottonView.xmg_height = 2;
    bottonView.xmg_x = 8;
    bottonView.xmg_width = 28;
    bottonView.tag = -12; // 區(qū)分按鈕
    bottonView.xmg_y = menuH - 2;
    bottonView.backgroundColor = textLineColor;
    [self.cateListScrollView addSubview:bottonView];
    self.iundicatyorView = bottonView;
    // 設(shè)置滑動范圍
    self.cateListScrollView.contentSize = CGSizeMake(widthContentSize + space, menuH);
}

#pragma mark - incident response
/** 點擊標(biāo)題按鈕 */
- (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;
    }];
    // 根據(jù)下標(biāo)滑動tableView對應(yīng)位置
    [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的時候調(diào)用此方法
- (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;
    }];
}

// 段頭即將出現(xiàn)
- (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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,313評論 4 61
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,160評論 22 665
  • 原文鏈接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影閱讀 33,165評論 6 472
  • 不知不覺中,人生已近半。 仿佛彈指一揮間, 我們的生命又畫上了一道年輪。 有怎樣的年齡,就有怎樣的人生使命; ...
    今生為誰閱讀 418評論 0 4
  • 對自己好點 不要自責(zé) 要努力才有選擇 雞湯喝多了就有了幻想 過好當(dāng)下 忘記不該記著的人 不要在觸景生情 忘記一件事...
    檒緣閱讀 119評論 0 0

友情鏈接更多精彩內(nèi)容