#define headerHeight 40
@interface TestTableViewController ()
{
//是否上拉
BOOL _isScrollUp;
NSMutableDictionary<NSString*,UIView*> *headerViews;
}
@end
@implementation TestTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
headerViews = [NSMutableDictionary new];
self.tableView.rowHeight = 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return headerHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.000000001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
NSString *key = [NSString stringWithFormat:@"%zd",section];
UIView *header = headerViews[key];
if (!header) {
header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, headerHeight)];
header.tag = section;
header.backgroundColor = [UIColor lightGrayColor];
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:@"第%zd组",section];
[label sizeToFit];
label.center = header.center;
[header addSubview:label];
//缓存组头
headerViews[key] = header;
//观察坐标变化
[header addObserver:self forKeyPath:@"frame" options:(NSKeyValueObservingOptionNew) context:nil];
}
return header;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 20;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%zd行",indexPath.row];
return cell;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
UIView *header = (UIView*)object;
//转化成相对屏幕的x坐标
CGRect rect = [self.tableView convertRect:header.frame toView:self.tableView.superview];
NSInteger tag = header.tag;
//上拉
if (_isScrollUp) {
CGFloat naviBarHeight = [self getNavigaHeight];
//最上面的headerview完全消失
if (rect.origin.y < naviBarHeight - headerHeight) {
[self currentHeader:tag+1];
}
}else{
CGFloat limitY = [self isFringe]?88:64;
//刘海屏为什么是88?不是84
///y坐标 = 导航条高, 刚好位于导航条下面,header完全出现
if (rect.origin.y == limitY ) {
[self currentHeader:tag];
}
}
}
///当前header
- (void)currentHeader:(NSInteger)tag{
NSString *key = [NSString stringWithFormat:@"%zd",tag];
UIView *view = headerViews[key];
[self reset];
if (view) {
[self shadow:view];
}
}
- (void)shadow:(UIView*)view{
view.layer.shadowColor = [UIColor redColor].CGColor;
// 阴影偏移量 默认为(0,3)
view.layer.shadowOffset = CGSizeMake(4, 4);
// 阴影透明度
view.layer.shadowOpacity = 1;
}
//恢复默认样式
- (void)reset{
NSArray *views = headerViews.allValues;
[views enumerateObjectsUsingBlock:^(UIView * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) {
// 阴影偏移量 默认为(0,3)
view.layer.shadowOffset = CGSizeZero;
// 阴影透明度
view.layer.shadowOpacity = 0;
}];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
static CGFloat lastOffsetY = 0;
_isScrollUp = lastOffsetY < scrollView.contentOffset.y;
lastOffsetY = scrollView.contentOffset.y;
}
//导航条高
- (CGFloat)getNavigaHeight{
if ([self isFringe]) {
return 84;
}
return 64;
}
//刘海屏
- (BOOL)isFringe{
BOOL iPhoneXSeries = NO;
if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
return iPhoneXSeries;
}
if (@available(iOS 11.0, *)) {
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
if (mainWindow.safeAreaInsets.bottom > 0.0) {
iPhoneXSeries = YES;
}
}
return iPhoneXSeries;
}
iOS 监听section 组头变化
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近做到项目时,使用到了TableView,其中需要设置组间距,然后我使用了Grouped样式的话,直接导致了下面...
- 上周读完了《运营之光,我的互联网运营方法论与自白》,收获颇丰,即包含道的内容也包含术的内容。 把我的读书笔记粘进来...