KVO tableView header悬浮效果

 

#import "ViewController.h"

#import "MyView.h"

 

#define KEYPATH @"contentOffset"

 

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

 

@property(nonatomic,strong)UITableView *tableView;

 

@property(nonatomic,strong)NSArray *dataArr;

 

@property(nonatomic,strong)MyView *headerView;

 

@end

 

@implementation ViewController

- (NSArray *)dataArr

{

    if (_dataArr == nil) {

        _dataArr = [NSArray array];

        _dataArr = @[@"12",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs",@"werd",@"sdfgd",@"fs"];

    }

    return _dataArr;

}

 

- (UITableView *)tableView

{

    if (_tableView == nil) {

        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

        _tableView.delegate = self;

        _tableView.dataSource = self;

        _tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);

        _tableView.backgroundColor =[UIColor lightGrayColor];

        [self.view addSubview:_tableView];

        

         [_tableView addObserver:self forKeyPath:KEYPATH options:NSKeyValueObservingOptionNew context:nil];

 

        self.headerView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];

        self.headerView.backgroundColor = [UIColor redColor];

        [self.view addSubview:self.headerView];

 

    }

    return _tableView;

}

#pragma make - KVO 回调方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context

{

    if ([keyPath isEqualToString:KEYPATH]) {

        CGPoint offset = [change[NSKeyValueChangeNewKey] CGPointValue];

        if (offset.y<=0 && -offset.y>=64) {

            CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, -offset.y);

            self.headerView.frame = newFrame;

            if (-offset.y <= 200) {

                _tableView.contentInset = UIEdgeInsetsMake(-offset.y, 0, 0, 0);

            }else{

                CGRect newFrame = CGRectMake(0, 0, self.view.frame.size.width, 64);

                self.headerView.frame = newFrame;

                _tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);

            }

        }

    }

    

}

 

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor orangeColor];

    [self tableView];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.dataArr.count;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *cellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }

    cell.textLabel.text = self.dataArr[indexPath.row];

    return cell;

}

 

 

@end

 

 

 

 

 

 

 

 

 

 

 

#import "MyView.h"

 

@implementation MyView

 

 

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

{

    UIView *result = [super hitTest:point withEvent:event];

    if (result == self) {

        return nil;

    } else {

        return result;

    }

}

 

 

 

@end

转载于:https://www.cnblogs.com/sunflower-lhb/p/5535293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值