在UIView中抠透明区域

       _alphaView = [[UIViewalloc]initWithFrame:self.view.bounds];

        _alphaView.backgroundColor = [[UIColorblackColor]colorWithAlphaComponent:0.7];


        UIBezierPath *path = [UIBezierPathbezierPathWithRect:_alphaView.frame];

        [path appendPath:[[UIBezierPathbezierPathWithRoundedRect: CGRectMake((UIScreenMainScreenWidth -200) * 0.5, (UIScreenMainScreenHeight -200) * 0.5 -100, 200,200) cornerRadius:0]bezierPathByReversingPath]];

        CAShapeLayer *shapeLayer = [CAShapeLayerlayer];

        shapeLayer.path = path.CGPath;

        [_alphaView.layersetMask:shapeLayer];

        [self.viewaddSubview:_alphaView];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于您的问题,您可以使用CAShapeLayer来创建一个空心遮盖层。具体的做法如下: 1.创建一个UIView,作为需要添加遮盖层的视图。 2.使用CAShapeLayer创建一个空心的遮盖层。 3.将CAShapeLayer添加到UIView的layer。 4.设置CAShapeLayer的path属性,使其形成一个空心的区域。 5.设置CAShapeLayer的fillColor属性为透明,strokeColor属性为您想要的颜色。 6.设置UIView的背景色为您想要的颜色,这样就能够看到空心区域的内容。 示例代码如下: ``` // 创建需要添加遮盖层的视图 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)]; view.backgroundColor = [UIColor whiteColor]; // 创建CAShapeLayer并添加到UIView的layer CAShapeLayer *maskLayer = [CAShapeLayer layer]; [view.layer addSublayer:maskLayer]; // 创建一个矩形,表示需要空心的区域 CGRect rect = CGRectMake(50, 50, 100, 100); // 创建一个圆形,表示需要透明区域 UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(75, 75, 50, 50)]; // 创建一个路径,表示空心的区域 UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect]; [path appendPath:circlePath]; [path setUsesEvenOddFillRule:YES]; // 设置CAShapeLayer的属性 maskLayer.path = path.CGPath; maskLayer.fillRule = kCAFillRuleEvenOdd; maskLayer.fillColor = [UIColor clearColor].CGColor; maskLayer.strokeColor = [UIColor redColor].CGColor; maskLayer.lineWidth = 2; // 将UIView添加到父视图 [self.view addSubview:view]; ``` 这样就可以创建一个空心的遮盖层,其圆形区域透明的,可以看到UIView的背景色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值