使用CAShapLayer制作半透明的镂空帮助页

半透明帮助页效果图

写了一个简单的demo
详细代码如下

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupShapLayer];
}

- (void)setupShapLayer {
    // 1.创建一个遮罩View,并添加
    UIView *maskView = [[UIView alloc] initWithFrame:self.view.bounds];
    maskView.backgroundColor = [UIColor blackColor];
    maskView.alpha = 0.6;
    [self.view addSubview:maskView];
    
    // 2.贝塞尔曲线 创建一个和 遮罩View 一样大小的 第一个 backgroudPath,并添加
    UIBezierPath *backgroudPath = [UIBezierPath bezierPathWithRect:self.view.bounds];
    // 贝塞尔曲线 创建第二个小矩形的 rectPath
    CGFloat width = 160;
    CGFloat height = 100;
    CGFloat x = (self.view.bounds.size.width - width) * 0.5;
    CGFloat y = 184;
    UIBezierPath *rectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x, y, width, height) cornerRadius:10];
    // rectPath 反转成矩形框以外的path
    rectPath = [rectPath bezierPathByReversingPath];
    // rectPath 
    rectPath.usesEvenOddFillRule = YES;
    // 3.
    [backgroudPath appendPath:rectPath];
    
    // 4.创建一个CAShapeLayer 图层
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = backgroudPath.CGPath;
    shapeLayer.fillColor = [UIColor yellowColor].CGColor; // 除了clearColor外任意颜色
    shapeLayer.lineWidth = 10;
    shapeLayer.borderColor = [UIColor redColor].CGColor;
    shapeLayer.borderWidth = 20;
    //添加图层蒙板
    maskView.layer.mask = shapeLayer;
}

然后在modal出来的视图的背景色为clearColor,再添加其它视图,注意view的层级就行了

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,956评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • 中国足球️在长沙众志成城赢了韩国,是个美好的日子,小鹤给大学舍友发了几个红包问候。下面做今天的分享: 前阵子有...
    快乐的小二仙阅读 205评论 0 0
  • 翻看下日历才发现已经秋天了,这月22号就秋分了。边看日历就边在想为啥现在夏天比我小时候的夏天过的快了。小时候生活在...
    京修阅读 151评论 0 0
  • 又一年又三年,再别高指导。 当2011年高洪波高指导被下课之时,他曾说过: “这是我在国足的上半场,我希望还有下半...
    杨一YoungOne阅读 403评论 2 2