Section透明时,隐藏背后重叠的Cell

  • Section Header 透明时
- (void)scrollViewDidScroll1:(UIScrollView *)scrollView {
    [_tableView.visibleCells enumerateObjectsUsingBlock:^(__kindof UITableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // cell和section覆盖的高度 = 需要剪去的高度 20是contentInset, 100是sectionHeight,再减去cell的y
        CGFloat hideCellHeight = scrollView.contentOffset.y + 20 + 100 - obj.frame.origin.y;
        
        CGRect maksFrame = obj.bounds;
        
        if (hideCellHeight > 0 && hideCellHeight <= obj.frame.size.height) {
            maksFrame =  CGRectMake(0, hideCellHeight, obj.frame.size.width, obj.frame.size.height - hideCellHeight);
        } else if (hideCellHeight > obj.frame.size.height) {
            maksFrame = CGRectZero;
        }
        [obj clipOutSideCellWhenUnderTransparencySectionSetMaskFrame:maksFrame];
    }];
}
  • Section Footer 透明时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [_tableView.visibleCells enumerateObjectsUsingBlock:^(__kindof UITableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        // cell和section覆盖的高度
        CGFloat hideCellHeight = obj.frame.origin.y - scrollView.contentOffset.y - scrollView.frame.size.height + obj.frame.size.height + 100;
        
        CGRect maksFrame = obj.bounds;
        
        NSLog(@"%@", @(hideCellHeight));
        
        if (hideCellHeight > 0 && hideCellHeight <= obj.frame.size.height) {
            maksFrame =  CGRectMake(0, -0, obj.frame.size.width, obj.frame.size.height - hideCellHeight);
        } else if (hideCellHeight > obj.frame.size.height) {
            maksFrame = CGRectZero;
        }
        [obj clipOutSideCellWhenUnderTransparencySectionSetMaskFrame:maksFrame];

    }];
}
  • Cell 代码
- (void)clipOutSideCellWhenUnderTransparencySectionSetMaskFrame:(CGRect)frame {
    CALayer *maskLayer = [CALayer layer];
    maskLayer.backgroundColor = [UIColor whiteColor].CGColor;
    maskLayer.frame = frame;
    self.layer.mask = maskLayer;
}

参考:https://www.jianshu.com/p/83fe305d2b57

推荐阅读更多精彩内容