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

前言

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

总的来说,还是比较简单的。下面是我遇到的过程中想到的几种方法

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);
 }

转载于:https://www.cnblogs.com/WillsonTu/p/5599929.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值