uiview maskview使用:中间透明的UIView实现的几种方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uiview maskview使用:中间透明的UIView实现的几种方法相关的知识,希望对你有一定的参考价值。

http://blog.csdn.net/jeffasd/article/details/50805312

https://jackliu17.github.io/2016/05/03/%E4%BD%BF%E7%94%A8maskView%E8%AE%BE%E8%AE%A1%E5%8A%A8%E7%94%BB%E5%8F%8A%E9%81%AE%E7%9B%96/

http://www.jianshu.com/p/191e2aae90b0

 

当用户第一次使用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);
 }

 


以上是关于uiview maskview使用:中间透明的UIView实现的几种方法的主要内容,如果未能解决你的问题,请参考以下文章

UIView maskView属性

给 UIView maskView 添加黑色阴影

使用 UIImageView 在 UIView 中切割透明孔

UI-切圆角透明度取消按钮点击高亮效果按钮文字带下划线

透明后如何检测 UIView 上的手势?

使用 maskView 设计动画