iOS cutout mask mask

#import "GuideView.h"

#define MLScreenH [UIScreen mainScreen].bounds.size.height
#define MLScreenW [UIScreen mainScreen].bounds.size.width

@implementation GuideView
- (instancetype)init{
    self = [super init];
    self.frame = CGRectMake(0, 0, MLScreenW, MLScreenH);
    [self drawBorderMaskLayer];
    return self;
}

- (void)drawBorderMaskLayer{
    //描边
    CGRect alphaRect = CGRectMake(10, 100, 100, 100);
    CGFloat cornerRadius = 10;
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.frame = self.bounds;
    borderLayer.lineWidth = 3;
    borderLayer.strokeColor = [UIColor redColor].CGColor;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    
    UIBezierPath *bezierPath=[UIBezierPath bezierPathWithRoundedRect:alphaRect
                                                        cornerRadius:cornerRadius];
    borderLayer.path = bezierPath.CGPath;
    
    [self.layer insertSublayer:borderLayer atIndex:0];
    
    
    {//镂空
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.bounds;
        maskLayer.fillColor = [[UIColor blackColor] colorWithAlphaComponent:0.8].CGColor;
        
        UIBezierPath *bezierPath=[UIBezierPath bezierPathWithRect:self.bounds];
        [bezierPath appendPath:[[UIBezierPath bezierPathWithRoundedRect:alphaRect cornerRadius:cornerRadius] bezierPathByReversingPath]];
        maskLayer.path = bezierPath.CGPath;
        
        [self.layer insertSublayer:maskLayer atIndex:0];
    }
}
@end

Guess you like

Origin blog.csdn.net/ForeverMyheart/article/details/117085130