创建想要实现镂空效果的背景
UIBezierPath * path = [ UIBezierPath bezierPathWithRoundedRect: [ UIScreen mainScreen] . bounds cornerRadius: 0 ] ;
设置镂空部分的路径
UIBezierPath * path1 = [ UIBezierPath bezierPathWithOvalInRect: myRect] ;
UIBezierPath * path1 = [ UIBezierPath bezierPathWithRect: myRect] ;
UIBezierPath * path1 = [ UIBezierPath bezierPath] ;
path1. lineWidth = 1 ;
[ path1 moveToPoint: topPoint] ;
[ path1 addLineToPoint: leftPoint] ;
[ path1 addLineToPoint: rightPoint] ;
[ path1 closePath] ;
路径叠加,并设置填充规则
[ path appendPath: path1] ;
[ path setUsesEvenOddFillRule: YES] ;
创建CAShapeLayer
CAShapeLayer * fillLayer = [ CAShapeLayer layer] ;
fillLayer. path = path. CGPath;
fillLayer. fillRule = kCAFillRuleEvenOdd;
fillLayer. fillColor = [ UIColor whiteColor] . CGColor;
fillLayer. opacity = 0.5 ;
[ self . layer addSublayer: fillLayer] ;
效果图