OC-开发案例收集

iOS 视图镂空效果(UIBezierPath,CAShapeL

2017-05-19  本文已影响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等使用场景的参考

上一篇 下一篇

猜你喜欢

热点阅读