How to mask UITableViewCells underneath a UITableView Transparent Header

后端 未结 10 1927
后悔当初
后悔当初 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:37

    Try to make a subclass of UITableviewCell and add these methods

    - (void)maskCellFromTop:(CGFloat)margin {
        self.layer.mask = [self visibilityMaskWithLocation:margin/self.frame.size.height];
        self.layer.masksToBounds = YES;
    }
    
    - (CAGradientLayer *)visibilityMaskWithLocation:(CGFloat)location {
        CAGradientLayer *mask = [CAGradientLayer layer];
        mask.frame = self.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;
    }
    

    and add this delegate method in UITableView

    #pragma mark - UIScrollViewDelegate
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        for (iNotifyTableViewCell *cell in self.visibleCells) {
            CGFloat hiddenFrameHeight = scrollView.contentOffset.y + [iNotifyHeaderView height] - cell.frame.origin.y;
            if (hiddenFrameHeight >= 0 || hiddenFrameHeight <= cell.frame.size.height) {
                [cell maskCellFromTop:hiddenFrameHeight];
            }
        }
    }
    

    *Note that [iNotifyHeaderView height] is the height of the HeaderView. and use #import for the custom cell.

提交回复
热议问题