この一年間やたらとUIBezierPathを使ったけど、稀によくあるUIViewいっぱいいっぱいに角丸枠線つけたい時に使うExtension
UIBezierPath+roundedCorner
extension UIBezierPath {
static func roundedCorners(rect: CGRect, topLeft: CGFloat, topRight: CGFloat, bottomRight: CGFloat, bottomLeft: CGFloat, lineWidth: CGFloat) -> UIBezierPath {
func radian(_ angle: CGFloat) -> CGFloat {
return angle * CGFloat(Double.pi) / 180
}
let path = UIBezierPath()
path.lineWidth = lineWidth
let halfWidth = lineWidth/2
path.move(to: CGPoint(x: topLeft + halfWidth, y: halfWidth))
path.addLine(to: CGPoint(x: rect.maxX - halfWidth - topRight, y: halfWidth))
path.addArc(withCenter: CGPoint(x: rect.maxX - halfWidth - topRight, y: topRight + halfWidth),
radius: topRight,
startAngle: radian(-90),
endAngle: radian(0),
clockwise: true)
path.addLine(to: CGPoint(x: rect.maxX - halfWidth, y: rect.maxY - halfWidth - bottomRight))
path.addArc(withCenter: CGPoint(x: rect.maxX - halfWidth - bottomRight, y: rect.maxY - halfWidth - bottomRight),
radius: bottomRight,
startAngle: radian(0),
endAngle: radian(90),
clockwise: true)
path.addLine(to: CGPoint(x: bottomLeft + halfWidth, y: rect.maxY - halfWidth))
path.addArc(withCenter: CGPoint(x: bottomLeft + halfWidth, y: rect.maxY - halfWidth - bottomLeft),
radius: bottomLeft,
startAngle: radian(90),
endAngle: radian(180),
clockwise: true)
path.addLine(to: CGPoint(x: halfWidth, y: topLeft + halfWidth))
path.addArc(withCenter: CGPoint(x: topLeft + halfWidth, y: topLeft + halfWidth),
radius: topLeft,
startAngle: radian(180),
endAngle: radian(270),
clockwise: true)
return path
}
}