悬浮导航标题,下面横放多个tableView的demo

悬浮导航标题,下面横放多个tableView的demo_第1张图片
TableInScrollView.gif

请关注,防止你用了,我改了,有问题连个商量的人都找不到...

废话少说,上代码

TableTopHeaderView.h

#import 

@interface TableTopHeaderView : UIView

@property (nonatomic, assign) CGFloat BottomItemH;

@property (nonatomic, assign) NSInteger selectedIndex;

@property (nonatomic, copy) void(^btnClickBlock)(NSInteger index);

@end

TableTopHeaderView.m

#import "TableTopHeaderView.h"
#import "DDYButton.h"
#import "DDYButton+DDYLinkBlock.h"

@interface TableTopHeaderView ()

@property (nonatomic, strong) UIView   *headView;

@property (nonatomic, strong) UIButton *firstBtn;

@property (nonatomic, strong) UIButton *secondBtn;

@property (nonatomic, strong) UIButton *thirdBtn;

@property (nonatomic, strong) UIView   *lineView;

@end

@implementation TableTopHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setupHeadView];
        [self setHorizontalBtn];
    }
    return self;
}

#pragma mark 这里设置上面显示的视图
- (void)setupHeadView
{
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DDYSCREENW, 200)];
    _headView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:_headView];
}

- (void)setHorizontalBtn
{
    CGFloat btnW = DDYSCREENW/3.0;
    
    _firstBtn  = [DDYButton customDDYBtn].btnTitleN(@"第一个按钮").btnBgColor(DDYColor(245, 245, 245, 1)).btnTag(101);
    _secondBtn = [DDYButton customDDYBtn].btnTitleN(@"第二个按钮").btnBgColor(DDYColor(245, 245, 245, 1)).btnTag(102);
    _thirdBtn  = [DDYButton customDDYBtn].btnTitleN(@"第三个按钮").btnBgColor(DDYColor(245, 245, 245, 1)).btnTag(103);
    _lineView  = UIViewNew.viewBGColor([UIColor redColor]).viewSetFrame(0, 240, btnW, 1);
    
    _firstBtn.btnFrame( 0*btnW, 200, btnW, 40).btnSuperView(self).btnTitleColorN([UIColor redColor]);
    _secondBtn.btnFrame(1*btnW, 200, btnW, 40).btnSuperView(self).btnTitleColorN([UIColor lightGrayColor]);
    _thirdBtn.btnFrame( 2*btnW, 200, btnW, 40).btnSuperView(self).btnTitleColorN([UIColor lightGrayColor]);
    
    [_firstBtn  addTarget:self action:@selector(handleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_secondBtn addTarget:self action:@selector(handleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_thirdBtn  addTarget:self action:@selector(handleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_lineView];
    _selectedIndex = 0;
    _BottomItemH = _firstBtn.ddy_h;
}

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    _selectedIndex = selectedIndex;
    
    if (_selectedIndex == 0)
    {
        _firstBtn.btnTitleColorN([UIColor redColor]);
        _secondBtn.btnTitleColorN([UIColor lightGrayColor]);
        _thirdBtn.btnTitleColorN([UIColor lightGrayColor]);
    }
    else if (_selectedIndex == 1)
    {
        _firstBtn.btnTitleColorN([UIColor lightGrayColor]);
        _secondBtn.btnTitleColorN([UIColor redColor]);
        _thirdBtn.btnTitleColorN([UIColor lightGrayColor]);
    }
    else if (_selectedIndex == 2)
    {
        _firstBtn.btnTitleColorN([UIColor lightGrayColor]);
        _secondBtn.btnTitleColorN([UIColor lightGrayColor]);
        _thirdBtn.btnTitleColorN([UIColor redColor]);
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        _lineView.ddy_x = _selectedIndex * DDYSCREENW/3.0;
    }];
}

- (void)handleBtnClick:(DDYButton *)button
{
    if (self.btnClickBlock)
    {
        self.btnClickBlock(button.tag-101);
    }
}

@end

TestTableView.h

#import 
#import "TableTopHeaderView.h"
#import "TableViewInScrollViewVC.h"

@interface TestTableView : UITableView

@property (nonatomic, strong) TableTopHeaderView *topView;

@property (nonatomic, strong) TableViewInScrollViewVC *currentVC;

@end

TestTableView.m

#import "TestTableView.h"

@interface TestTableView ()

@end

@implementation TestTableView

- (void)setTopView:(TableTopHeaderView *)topView
{
    _topView = topView;
    
    self.dataSource = self;
    self.delegate = self;
    self.scrollIndicatorInsets = UIEdgeInsetsMake(self.topView.ddy_h, 0, 0, 0);
    self.tableHeaderView = UIViewNew.viewSetFrame(0, 0, DDYSCREENW, self.topView.ddy_h);
    self.tableFooterView = [UIView new];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

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

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return (section == 2) ? 0.1 : 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellID = @"reuseFirstTableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld--%ld", (long)indexPath.section, (long)indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DDYLog(@"didSelect:%ld--%ld",(long)indexPath.section, (long)indexPath.row);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat topViewContentH = self.topView.ddy_h - self.topView.BottomItemH;
    
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY >= 0 && offsetY <= topViewContentH)
    {
        self.topView.ddy_y = -offsetY;
    }
    else if (offsetY > topViewContentH)
    {
        self.topView.ddy_y = -topViewContentH;
    }
    else if (offsetY <0)
    {
        self.topView.ddy_y =  -offsetY;
    }
}

@end

TableViewInScrollViewVC

#import "TableViewInScrollViewVC.h"
#import "TableTopHeaderView.h"
#import "TestTableView.h"

@interface TableViewInScrollViewVC ()

@property (nonatomic, strong) UIScrollView *bottomScrollView;

@property (nonatomic, strong) TableTopHeaderView *headView;

@property (nonatomic, strong) TestTableView *firstTableView;

@property (nonatomic, strong) TestTableView *secondTableView;

@property (nonatomic, strong) TestTableView *thirdTableView;

@property (nonatomic, strong) UIView *navLine;

@end

@implementation TableViewInScrollViewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self prepare];
    [self.view addSubview:self.bottomScrollView];
    [self.view addSubview:self.headView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _navLine.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    _navLine.hidden = NO;
}

- (void)prepare
{
    // 64当起点布局
    UIView *backgroundView = [self.navigationController.navigationBar subviews].firstObject;
    for (UIView *view in backgroundView.subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.ddy_h == 0.5)
        {
            _navLine = (UIImageView *)view;
        }
    }
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.view.backgroundColor = DDYColor(245, 245, 245, 1);
    self.navigationController.navigationBar.barTintColor = DDYColor(247, 247, 247, 1);
}

- (UIScrollView *)bottomScrollView
{
    if (!_bottomScrollView)
    {
        _bottomScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _bottomScrollView.contentSize = CGSizeMake(DDYSCREENW*3, 0);
        _bottomScrollView.showsVerticalScrollIndicator = NO;
        _bottomScrollView.showsHorizontalScrollIndicator = NO;
        _bottomScrollView.pagingEnabled = YES;
        _bottomScrollView.delegate = self;
        _bottomScrollView.bounces = NO;
        [_bottomScrollView addSubview:self.firstTableView];
        [_bottomScrollView addSubview:self.secondTableView];
        [_bottomScrollView addSubview:self.thirdTableView];
    }
    return _bottomScrollView;
}

- (TableTopHeaderView *)headView
{
    if (!_headView)
    {
        __weak __typeof__ (self)weakSelf = self;
        _headView = [[TableTopHeaderView alloc] initWithFrame:CGRectMake(0, 0, DDYSCREENW, 241)];
        _headView.btnClickBlock = ^(NSInteger index) {
            [weakSelf verticalScrollSetting];
            weakSelf.headView.selectedIndex = index;
            [weakSelf.bottomScrollView setContentOffset:CGPointMake(index*DDYSCREENW, 0) animated:NO];
        };
    }
    return _headView;
}

- (TestTableView *)firstTableView
{
    if (!_firstTableView)
    {
        _firstTableView = [[TestTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _firstTableView.ddy_x = 0*DDYSCREENW;
        _firstTableView.ddy_h = DDYSCREENH-64;
        _firstTableView.topView = self.headView;
        
    }
    return _firstTableView;
}

- (TestTableView *)secondTableView
{
    if (!_secondTableView)
    {
        _secondTableView = [[TestTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _secondTableView.ddy_x = 1*DDYSCREENW;
        _secondTableView.ddy_h = DDYSCREENH-64;
        _secondTableView.topView = self.headView;
    }
    return _secondTableView;
}

- (TestTableView *)thirdTableView
{
    if (!_thirdTableView)
    {
        _thirdTableView = [[TestTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _thirdTableView.ddy_x = 2*DDYSCREENW;
        _thirdTableView.ddy_h = DDYSCREENH-64;
        _thirdTableView.topView = self.headView;
    }
    return _thirdTableView;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.navigationController.navigationBarHidden = (scrollView.contentOffset.y > 0);
    [self verticalScrollSetting];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self verticalScrollSetting];
    self.headView.selectedIndex = ceilf(scrollView.contentOffset.x / DDYSCREENW);
}

- (void)verticalScrollSetting
{
    CGFloat placeholderOffset = 0;
    if (self.headView.selectedIndex == 0)
    {
        if (self.firstTableView.contentOffset.y > self.headView.ddy_h - self.headView.BottomItemH)
        {
            placeholderOffset = self.headView.ddy_h - 40;
        }
        else
        {
            placeholderOffset = self.firstTableView.contentOffset.y;
        }
        [self.secondTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
        [self.thirdTableView  setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
    }
    else if (self.headView.selectedIndex == 1)
    {
        if (self.secondTableView.contentOffset.y > self.headView.ddy_h - self.headView.BottomItemH)
        {
            placeholderOffset = self.headView.ddy_h - self.headView.BottomItemH;
        }
        else
        {
            placeholderOffset = self.secondTableView.contentOffset.y;
        }
        [self.firstTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
        [self.thirdTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
    }
    else if (self.headView.selectedIndex == 2)
    {
        if (self.thirdTableView.contentOffset.y > self.headView.ddy_h - self.headView.BottomItemH)
        {
            placeholderOffset = self.headView.ddy_h - self.headView.BottomItemH;
        }
        else
        {
            placeholderOffset = self.thirdTableView.contentOffset.y;
        }
        [self.firstTableView  setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
        [self.secondTableView setContentOffset:CGPointMake(0, placeholderOffset) animated:NO];
    }
}

@end

点星星 scan demoCode

你可能感兴趣的:(悬浮导航标题,下面横放多个tableView的demo)