Mask all views below a UIView except background

2019-09-11 19:33发布

问题:

I have a transparent section header in a UITableView, which should let the cells scroll behind it but show the background image which is behind the tableview instead of the cells. I need a way to apply some sort of clipping mask to the section header which would make any views which go beneath it transparent, or something which shows the appropriate part of the background image as the header's background and follows when the header moves.

I have seen this How to mask UITableViewCells underneath a UITableView Transparent Header, but it doesn't quite work as advertised, and is generally a bad implementation.

I have also tried this:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, self.frame);
    CGContextSetBlendMode(context, kCGBlendModeClear);
}

in the section header's subclass, but this just shows the view with a black background.

If you need more explanation, here is what I have:

http://imgur.com/a/YM5Us

and here is what I want to achieve:

http://imgur.com/a/o9CgL

回答1:

I've done a similar task in a game. SpriteKit offers excellent performance on this since it has this as a built-in feature.

You can mix SpriteKit with UIKit but using SpriteKit as a background & not the other way around as you need, since SKView cannot have transparent background.

In my opinion you should here create a custom header view or to change the displaying of the cell.