带镂空效果的引导页
2017-10-25 本文已影响43人
WSGNSLog
代码不多,demo见github:https://github.com/WSGNSLog/MaskView
UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
maskView.backgroundColor = [UIColor blackColor];
maskView.alpha = 0.4;
[window addSubview:maskView];
//画一个矩形
UIBezierPath *bpath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) cornerRadius:0];
//画一个矩形,如果想保留矩形以外的部分 , 矩形需要反方向绘制path .
[bpath appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, y, 150, 48) cornerRadius:20] bezierPathByReversingPath]];
//创建一个CAShapeLayer 图层
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = bpath.CGPath;
//添加图层蒙板
maskView.layer.mask = shapeLayer;
UIImageView *guideImgV = [[UIImageView alloc]initWithFrame:CGRectMake(guideImgV_x, guideImgV_y, guideImgV_w, guideImgV_h)];
guideImgV.image = [UIImage imageNamed:@"hotLiveGuideImg"];
[maskView addSubview:guideImgV];