美文网首页
为 View 设置圆角镂空矩形

为 View 设置圆角镂空矩形

作者: 吕建雄 | 来源:发表于2023-10-11 14:26 被阅读0次

 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

相关文章

网友评论

      本文标题:为 View 设置圆角镂空矩形

      本文链接:https://www.haomeiwen.com/subject/oqagbdtx.html