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