ios 如何到达如下图的效果

如图图片描述

它的整个背景是 黑色透明度为0.5 然后只有圆形那块是没有背景色的
求解答

阅读 4k
6 个回答

用CAShapeLayer与UIBezierPath来画。

    CGRect frame = _scanRectView.frame;
    CGFloat detaSpace = 3.f;
    frame.origin.x += detaSpace;
    frame.origin.y += detaSpace;
    frame.size.height -= detaSpace * 2.0;
    frame.size.width -= detaSpace * 2.0;

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_scanBgView.bounds cornerRadius:0];
    UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:frame];
    [path appendPath:rectPath];
    [path setUsesEvenOddFillRule:YES];

    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [UIColor colorWithWhite:0.0 alpha:0.8].CGColor;
    fillLayer.opacity = 0.5;

    [_scanBgView.layer addSublayer:fillLayer];

这是我用到的画方形的代码。

可以画一个圆角啊

最简单的方法:
叫 UI 给你一个背景半透明,中间一个透明圆的图片。。。2333

有很多上传头像的库,里面就有解决方案。

找你们设计师 给你出一张png,中间透明,其他部分半透明。这样最快 其他的你可以考虑layer的图形绘制 具体方法,估计你应该知道

新手上路,请多包涵

如果尺寸小的话可以设layer的半角,如果尺寸大的话CAShapeLayer。如果最省事的话管UI要

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题