iOS tableview頭部懸浮

demo主要核心部分是將view添加到tableView上,然后通過監(jiān)聽滾動(dòng)來實(shí)現(xiàn).代碼量少而簡

效果圖

image.png
#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController
@end
#import "ViewController.h"
#import <MJRefresh/MJRefresh.h>
#define HeightHead 200.0
#define HeightPlaceholder 80.0

@interface ViewController ()
@property (nonatomic, strong) UIImageView *headImageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11.0, *))
    {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
    // tableHeaderView
    UIView *tableHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, HeightHead)];
    tableHead.hidden = YES;
    self.tableView.tableHeaderView = tableHead;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(HeightHead, 0, 0, 0);
    // 刷新控件
    __weak __typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header.ignoredScrollViewContentInsetTop = -HeightHead;
    //懸停view
    [self.tableView addSubview:self.headImageView];
}

#pragma mark - Table view data source

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    
    return cell;
}

- (UIImageView *)headImageView {
    if (_headImageView == nil) {
        _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, HeightHead)];
        _headImageView.backgroundColor = [UIColor redColor];
        _headImageView.image = [UIImage imageNamed:@"head.bundle/saber.jpg"];
        UILabel *placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, HeightHead - HeightPlaceholder, self.view.bounds.size.width, HeightPlaceholder)];
        placeLabel.text = @"我是懸停";
        placeLabel.textColor = [UIColor whiteColor];
        placeLabel.textAlignment = NSTextAlignmentCenter;
        placeLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        [_headImageView addSubview:placeLabel];
    }
    return _headImageView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat placeHolderHeight =  HeightHead - HeightPlaceholder;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY <= 0) {
        self.headImageView.frame = CGRectMake(0, offsetY, self.headImageView.bounds.size.width, self.headImageView.bounds.size.height);
    }else if (offsetY > placeHolderHeight){
        self.headImageView.frame = CGRectMake(0, offsetY - placeHolderHeight, self.headImageView.bounds.size.width, self.headImageView.bounds.size.height);
    }else{
        self.headImageView.frame = CGRectMake(0, 0, self.headImageView.bounds.size.width, self.headImageView.bounds.size.height);
    }
}

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

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