Clicky

iOS Dev Nugget 259 CAShapeLayer, UIBezierPath and UIRectCorner

.

Need to run a code review on your codebase? Hire me

In #135 CAShapeLayer, Animated Views and XCPShowView(), we spoke about using CALayer and UIBezierPath to draw and animate outlines.

Here's another common thing you can do with it — Rounded corners:

let canvas = UIView()
canvas.frame = CGRect(x: 0, y: 0, width: 200, height: 300)
canvas.backgroundColor = .blue

let v2 = UIView()
v2.frame = CGRect(x: 20, y: 20, width: 50, height: 60)
v2.backgroundColor = .red
v2.layer.cornerRadius = 8
canvas.addSubview(v2)

And if you only want to round certain corners, use UIRectCorner:

let v3 = UIView()
v3.frame = CGRect(x: 20, y: 100, width: 50, height: 60)
v3.backgroundColor = .green
canvas.addSubview(v3)

let cornerRadius = 12
let layer = CAShapeLayer()
layer.path = UIBezierPath(roundedRect: v3.bounds, byRoundingCorners: [.topLeft, .bottomRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)).cgPath
v3.layer.mask = layer

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues