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

2021-09-02  本文已影响0人  iVikings
- (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];
    }];
}
- (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];

    }];
}
- (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

上一篇 下一篇

猜你喜欢

热点阅读