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

效果

6EA2810BB4DD61E59AC0AD376000833F 2.jpg

拓展

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 24,248评论 8 184
  • 停下脚步,享受当下,伫目欣赏,平静内心,做一个淡雅不俗的自己。瑜伽是一种生活方式,是修身养性,是一种和谐的境界。
    暖风徐徐_8e6f阅读 2,415评论 0 4
  • 下山的路上,刘叔指给我树梢上一些星星点点的黄色:“木姜子花都开了,摘下来烘干,泡水喝可以治咳嗽。”凝神细看才发现,...
    shyaen阅读 4,162评论 0 1