中间透明的UIView实现的几种方法

前言

当用户第一次使用app的时候, 在app的开始页或者主要的界面,就会显示用户引导,引导用户怎么使用这个app。就类似下面这种界面(随便找张QQ的截图做为app的界面)


Paste_Image.png

总的来说,还是比较简单的。下面是我上网找的几种方法

1、用CALayer的mask层来实现

   UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.view.bounds];
    [path addArcWithCenter:CGPointMake(CGRectGetWidth(self.view.bounds)/2, CGRectGetHeight(self.view.bounds)/2) radius:50 startAngle:0 endAngle:M_PI *2 clockwise:YES];
    path.usesEvenOddFillRule = YES;
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor= [UIColor blackColor].CGColor;  //其他颜色都可以,只要不是透明的
    shapeLayer.fillRule=kCAFillRuleEvenOdd;
  
    UIView *translucentView = [UIView new];
    translucentView.frame = self.imaegView.bounds;
    translucentView.backgroundColor = [UIColor blackColor];
    translucentView.alpha = 0.5;
    translucentView.layer.mask = shapeLayer;
    
    [self.view addSubview:translucentView];

    //把shapeLayer的透明度改为0.5,直接添加layer也一样
    // shapeLayer.opacity = 0.5
    //[self.imageView.layer addSublayer:shapeLayer];

2、使用透明图片作为UIView来实现

    UIView *translucentView = [UIView new];
    translucentView.frame = self.imaegView.bounds;
    translucentView.backgroundColor = [UIColor blackColor];
    translucentView.alpha = 0.5;
    
    UIImageView *maskView = [UIImageView new];
    maskView.frame = self.imaegView.bounds;
    maskView.image = [UIImage imageNamed:@"translucent"];
    maskView.contentMode = UIViewContentModeScaleToFill;
//    maskView.
    
    translucentView.maskView = maskView;
    [self.imaegView addSubview:translucentView];

3、使用drawRect:(CGRect)rect实现(参考别人的)

- (void)drawRect:(CGRect)rect{ //创建路径并获取句柄 
    CGMutablePathRef path = CGPathCreateMutable(); //指定矩形 
    CGRect rectangle = self.bounds; //将矩形添加到路径中 
    CGPathAddRect(path,NULL, rectangle); //获取上下文 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); //将路径添加到上下文
    CGContextAddPath(currentContext, path); //设置矩形填充色 CGContextSetFillColorWithColor(currentContext, 
    [UIColor colorWithWhite:0.0f alpha:0.8f].CGColor); 
    CGContextFillRect(currentContext, rectangle); 
    CGContextClearRect(currentContext, CGRectMake(50.f, 50.f, 220.f, 220.f));//绘制
    CGContextDrawPath(currentContext, kCGPathFillStroke); 
    CGPathRelease(path);
 }```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,196评论 4 61
  • 以下是偶然翻出来的2014-2015年之间,为某朋友的公司写给企业的营销型网站建设方案,内容是东抄西抄的,主要目的...
    甲方爸爸肖运华阅读 860评论 1 4
  • 相遇它也许是美妙而青涩,又或者是痛不欲生,无法释怀的经历,但它绝对是独一无二。 我和他起初是因为工作在网上认识的,...
    柒樾阅读 392评论 4 3
  • 想来很久以前,撬茶这件事是很不屑于做的,但苦恼于圆圆的茶饼子不知怎么掰开,粗鲁又血腥的又掰又摔,常常搞得自己...
    Z小槿阅读 772评论 0 1
  • “一月肉更肥,二月不知谁,三月不减肥,四月徒伤悲,五月路人雷,六月男友没,七月被晒黑,八月待室内,九月更加肥,十月...
    草上霜阅读 1,193评论 0 2