let path =UIBezierPath(rect:CGRect(x:0,y:0,width:ScreenWidth,height:ScreenHeight))
//镂空区域
let rect :CGRect=CGRect(x:16~,y: y,width:ScreenWidth-32~,height: height)
//圆角大小
let cornerRadius =12~
let topLeft =CGPoint(x:CGRectGetMinX(rect) ,y:CGRectGetMinY(rect))
let bottomLeft =CGPoint(x:CGRectGetMinX(rect),y:CGRectGetMaxY(rect))
let bottomRight =CGPoint(x:CGRectGetMaxX(rect),y:CGRectGetMaxY(rect))
let topRight =CGPoint(x:CGRectGetMaxX(rect),y:CGRectGetMinY(rect))
//逆时针绘制圆角矩形
path.move(to:CGPoint(x: topLeft.x+ cornerRadius,y: topLeft.y))
path.addArc(withCenter:CGPoint(x: topLeft.x+ cornerRadius,y: topLeft.y+ cornerRadius),radius: cornerRadius,startAngle:CGFloat(-Double.pi/2),endAngle:CGFloat(Double.pi),clockwise:false)
path.addLine(to:CGPoint(x: bottomLeft.x,y: bottomLeft.y- cornerRadius))
path.addArc(withCenter:CGPoint(x: bottomLeft.x+ cornerRadius,y: bottomLeft.y- cornerRadius),radius: cornerRadius,startAngle:CGFloat(Double.pi),endAngle:CGFloat(Double.pi/2),clockwise:false)
path.addLine(to:CGPoint(x: bottomRight.x- cornerRadius,y: bottomRight.y))
path.addArc(withCenter:CGPoint(x: bottomRight.x- cornerRadius,y: bottomRight.y- cornerRadius),radius: cornerRadius,startAngle:CGFloat(Double.pi/2),endAngle:0,clockwise:false)
path.addLine(to:CGPoint(x: topRight.x,y: topRight.y+ cornerRadius))
path.addArc(withCenter:CGPoint(x: topRight.x- cornerRadius,y: topRight.y+ cornerRadius),radius: cornerRadius,startAngle:0,endAngle:CGFloat(-Double.pi/2),clockwise:false)
path.close()
//新建 shapeLayer
let shaperLayer =CAShapeLayer()
//设置 shaperLayer 的 path 为 path
shaperLayer.path= path.cgPath
//将目标 View ,layer的 mask 设置为shaperLayer(superView 为要添加镂空的目标 view)
superView.layer.mask= shaperLayer
网友评论