您可以使用此扩展获得 bezierPath。
let path = UIBezierPath(yourView.bounds, cornerRadius: 12)
let layer = CAShapeLayer()
layer.masksToBounds = false
layer.path = path.cgPath
layer.strokeColor = UIColor.red.cgColor
layer.fillColor = UIColor.clear.cgColor
layer.lineWidth = 5
layer.addSublayer(topLayer)
extension UIBezierPath {
static func borderPathInRect(_ drawRect: CGRect, cornerRadius: CGFloat) -> UIBezierPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: drawRect.origin.x, y: cornerRadius))
path.addArc(withCenter: CGPoint(x: cornerRadius, y: cornerRadius),
radius: cornerRadius,
startAngle: CGFloat.pi,
endAngle: 3/2*CGFloat.pi ,
clockwise: true)
path.addLine(to: CGPoint(x: drawRect.size.width - cornerRadius, y: 0))
path.addArc(withCenter: CGPoint(x: drawRect.size.width - cornerRadius, y: cornerRadius),
radius: cornerRadius,
startAngle: 3/2*CGFloat.pi,
endAngle: 2*CGFloat.pi,
clockwise: true)
path.addLine(to: CGPoint(x: drawRect.size.width, y: drawRect.size.height - cornerRadius))
path.addArc(withCenter: CGPoint(x: drawRect.size.width - cornerRadius, y: drawRect.size.height - cornerRadius),
radius: cornerRadius,
startAngle: 0,
endAngle: CGFloat.pi/2,
clockwise: true)
path.addLine(to: CGPoint(x: cornerRadius, y: drawRect.size.height))
path.addArc(withCenter: CGPoint(x: cornerRadius, y: drawRect.size.height - cornerRadius),
radius: cornerRadius,
startAngle: CGFloat.pi/2,
endAngle: CGFloat.pi,
clockwise: true)
path.addLine(to: CGPoint(x: 0, y: cornerRadius))
path.lineJoinStyle = .round
return path
}
}