iOS 监听section 组头变化

#define headerHeight 40

@interface TestTableViewController ()
{
    //是否上拉
    BOOL _isScrollUp;
    NSMutableDictionary *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 *)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 组头变化)