中间透明的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);
 }
posted @ 2016-06-20 10:56  WillsonTu  阅读(1048)  评论(0编辑  收藏  举报