How to mask UITableViewCells underneath a UITableView Transparent Header

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

    I have two possible solutions, no code - just the idea:

    1. not generic, should work with the settup/design apple uses at the Notification Center.

    Make the Section-Header opaque, 'clone' the background-pattern of the table as background of the section-Header. Position the background-pattern depending on the section-header offset.


    1. genereic, but probably more performance problems. Should work fine with few cells.

    Add a Alpha Mask to all cell-layers. Move the Alpha Mask depending on the cell-position.


    (use scrollViewDidScroll delegate method to maintain the background-pattern / Alpha-Mask offset).

    0 讨论(0)
  • 2020-12-08 07:34

    @Alex Markman - your answer is great and was very usefull for me, but I found that when you're scrolling on retina devices, the cells do not scroll smoothly. I found that it is caused by layer's locations parameter during the rendering process:

    mask.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:location];

    I slightly modified your code. Maybe someone will find it useful:

    - (void)maskCellFromTop:(CGFloat)margin
    {
        self.layer.mask = [self visibilityMaskFromLocation:margin];
        self.layer.masksToBounds = YES;
    }
    
    - (CAGradientLayer *)visibilityMaskFromLocation:(CGFloat)location
    {
        CAGradientLayer *mask = [CAGradientLayer layer];
        mask.frame = CGRectMake(
                                self.bounds.origin.x,
                                location+self.bounds.origin.y,
                                self.bounds.size.width,
                                self.bounds.size.height-location);
        mask.colors = @[
                        (id)[[UIColor colorWithWhite:1 alpha:1] CGColor],
                        (id)[[UIColor colorWithWhite:1 alpha:1] CGColor]
                        ];
        return mask;
    }
    
    0 讨论(0)
  • 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 <QuartzCore/QuartzCore.h> for the custom cell.

    0 讨论(0)
  • 2020-12-08 07:37

    Swift version

    func scrollViewDidScroll(_ scrollView: UIScrollView) { 
        for cell in tableView.visibleCells {
            let hiddenFrameHeight = scrollView.contentOffset.y + navigationController!.navigationBar.frame.size.height - cell.frame.origin.y
            if (hiddenFrameHeight >= 0 || hiddenFrameHeight <= cell.frame.size.height) {
                maskCell(cell: cell, margin: Float(hiddenFrameHeight))
            }
        }
    }
    
    func maskCell(cell: UITableViewCell, margin: Float) {
        cell.layer.mask = visibilityMaskForCell(cell: cell, location: (margin / Float(cell.frame.size.height) ))
        cell.layer.masksToBounds = true
    }
    
    func visibilityMaskForCell(cell: UITableViewCell, location: Float) -> CAGradientLayer {
        let mask = CAGradientLayer()
        mask.frame = cell.bounds
        mask.colors = [UIColor(white: 1, alpha: 0).cgColor, UIColor(white: 1, alpha: 1).cgColor]
        mask.locations = [NSNumber(value: location), NSNumber(value: location)]
        return mask;
    }
    
    0 讨论(0)
  • 2020-12-08 07:43

    I ended up setting the height of the section header to its minimum, and overriding UITableView's layoutSubviews to place the header on the tableView's superview, adjusting the frame's origin upward by its height.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题