BezierPath倒圆角

使用:
imageView.cornerClip(topLeft: 30,bottomRight: 30)

效果:


BezierPath倒圆角_第1张图片
1594720717130.jpg

分类代码
extension UIView {
func cornerClip(topLeft:CGFloat = 0,topRight:CGFloat = 0,bottomLeft:CGFloat = 0,bottomRight:CGFloat = 0) {
let maskLayer = CAShapeLayer()
let maskPath = bezierPath(rect: self.bounds, topLeft: topLeft,topRight: topRight,bottomLeft: bottomLeft,bottomRight: bottomRight)
maskLayer.frame = self.bounds
maskLayer.path = maskPath
self.layer.mask = maskLayer
}
private func bezierPath(rect:CGRect,topLeft:CGFloat = 0,topRight:CGFloat = 0,bottomLeft:CGFloat = 0,bottomRight:CGFloat = 0) -> CGPath{
let topLeftCenterX = rect.minX + topLeft
let topLeftCenterY = rect.minY + topLeft
let topRightCenterX = rect.maxX - topRight
let topRightCenterY = rect.minY + topRight
let bottomLeftCenterX = rect.minX + bottomLeft
let bottomLeftCenterY = rect.maxY - bottomLeft
let bottomRightCenterX = rect.maxX - bottomRight
let bottomRightCenterY = rect.maxY - bottomRight
let path = CGMutablePath()
path.addArc(center: CGPoint(x: topLeftCenterX, y: topLeftCenterY), radius: topLeft, startAngle: .pi, endAngle: .pi/2 * 3, clockwise: false)
path.addArc(center: CGPoint(x: topRightCenterX, y: topRightCenterY), radius: topRight, startAngle: .pi/2 * 3, endAngle: 0, clockwise: false)
path.addArc(center: CGPoint(x: bottomRightCenterX, y: bottomRightCenterY), radius: bottomRight, startAngle: 0, endAngle: .pi/2, clockwise: false)
path.addArc(center: CGPoint(x: bottomLeftCenterX, y: bottomLeftCenterY), radius: bottomLeft, startAngle: .pi/2, endAngle: .pi, clockwise: false)
path.closeSubpath()
return path
}
}

你可能感兴趣的:(BezierPath倒圆角)