How to mask UITableViewCells underneath a UITableView Transparent Header

后端 未结 10 1921
后悔当初
后悔当初 2020-12-08 07:29

I want the header to mask the cells, but not the background.

I have a UITableView with transparent headers and cells similar to Apple\'s Notification Center (when yo

10条回答
  •  自闭症患者
    2020-12-08 07:45

    A little edit on Alex Markman's answer, where you could skip creating a subclass for an UITableViewCell. Benefit of this approach is that you can use it for multiple different UITableViewCells.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        for (UITableViewCell *cell in self.tableView.visibleCells) {
            CGFloat hiddenFrameHeight = scrollView.contentOffset.y + self.navigationController.navigationBar.frame.size.height - cell.frame.origin.y;
            if (hiddenFrameHeight >= 0 || hiddenFrameHeight <= cell.frame.size.height) {
                [self maskCell:cell fromTopWithMargin:hiddenFrameHeight];
            }
        }
    }
    
    - (void)maskCell:(UITableViewCell *)cell fromTopWithMargin:(CGFloat)margin
    {
        cell.layer.mask = [self visibilityMaskForCell:cell withLocation:margin/cell.frame.size.height];
        cell.layer.masksToBounds = YES;
    }
    
    - (CAGradientLayer *)visibilityMaskForCell:(UITableViewCell *)cell withLocation:(CGFloat)location
    {
        CAGradientLayer *mask = [CAGradientLayer layer];
        mask.frame = cell.bounds;
        mask.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:1 alpha:0] CGColor], (id)[[UIColor colorWithWhite:1 alpha:1] CGColor], nil];
        mask.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:location], [NSNumber numberWithFloat:location], nil];
        return mask;
    }
    

提交回复
热议问题