iOS 视图镂空效果(UIBezierPath,CAShapeLayer)

代码

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.view.bounds];
    // 创建矩形
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithRect:CGRectMake(self.view.bounds.size.width / 2 - 100, self.view.bounds.size.height / 2 - 150, 200, 200)];
    [path appendPath:circlePath];   
     
    CAShapeLayer *shaperLayer = [CAShapeLayer layer];
    shaperLayer.frame = self.view.bounds;
    shaperLayer.fillColor = [UIColor dd_hexStringToColor:@"000000" alpha:0.3].CGColor;
    // 设置填充规则
    shaperLayer.fillRule = kCAFillRuleEvenOdd;
    shaperLayer.path = path.CGPath;

    [self.view.layer addSublayer: shaperLayer];
}

效果

iOS 视图镂空效果(UIBezierPath,CAShapeLayer)_第1张图片
6EA2810BB4DD61E59AC0AD376000833F 2.jpg

拓展

一篇关于UIBezierPath,CAShapeLayer等使用场景的参考

你可能感兴趣的:(iOS 视图镂空效果(UIBezierPath,CAShapeLayer))