美文网首页
Section透明时,隐藏背后重叠的Cell

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

作者: iVikings | 来源:发表于2021-09-02 19:34 被阅读0次
  • 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

相关文章

网友评论

      本文标题:Section透明时,隐藏背后重叠的Cell

      本文链接:https://www.haomeiwen.com/subject/rfucwltx.html