代碼不多丰滑,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];
//畫一個(gè)矩形
UIBezierPath *bpath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) cornerRadius:0];
//畫一個(gè)矩形胃珍,如果想保留矩形以外的部分 , 矩形需要反方向繪制path .
[bpath appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, y, 150, 48) cornerRadius:20] bezierPathByReversingPath]];
//創(chuàng)建一個(gè)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];