ios 如何到达如下图的效果

图片说明

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

一个是设置backgroundColor为clearColor,就是设置好View为圆形范围

做一个黑色半透明蒙版,中间画出一个圆形的透明区就可以了

这个图层叠加做不出这个效果,可以把这个背景直接绘图,也就是绘制一个view,中间透明,边上半透明,

//获取需要的样式图片

  • (UIImage *)getImage{
    UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, NO, 1.0);
    CGContextRef con = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(con, [UIColor lightGrayColor].CGColor);//背景色
    CGContextFillRect(con, [UIScreen mainScreen].bounds);

    CGContextAddEllipseInRect(con, CGRectMake(50, 100, 200, 200));
    CGContextSetBlendMode(con, kCGBlendModeClear);
    CGContextFillPath(con);

    UIImage *ima = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return ima;

}

//将样式图片添加到VIew上

  • (void)addImage{ UIImageView *imageV = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds]; imageV.image = [self getImage]; imageV.alpha = 0.5; [self.view addSubview:imageV]; }

view中间画出一块透明原型区域就好