美文网首页OC-开发案例收集
iOS 视图镂空效果(UIBezierPath,CAShapeL

iOS 视图镂空效果(UIBezierPath,CAShapeL

作者: 师从小马哥 | 来源:发表于2017-05-19 11:41 被阅读548次

代码

- (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];
}

效果

6EA2810BB4DD61E59AC0AD376000833F 2.jpg

拓展

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

相关文章

网友评论

    本文标题:iOS 视图镂空效果(UIBezierPath,CAShapeL

    本文链接:https://www.haomeiwen.com/subject/wahyxxtx.html