Skip to content

Instantly share code, notes, and snippets.

@henrik-dmg
Last active January 10, 2019 08:22
Show Gist options
  • Save henrik-dmg/eae10abe8ca12041cbd63d3d399e2ce9 to your computer and use it in GitHub Desktop.
Save henrik-dmg/eae10abe8ca12041cbd63d3d399e2ce9 to your computer and use it in GitHub Desktop.
extension UIView {
func roundCorners(_ corners: Corners, radius: CGFloat) {
var cornerMasks = [CACornerMask]()
// Top left corner
switch corners {
case .all, .top, .topLeft, .allButTopRight, .allButBottomLeft, .allButBottomRight, .topLeftBottomRight:
cornerMasks.append(CACornerMask(rawValue: UIRectCorner.topLeft.rawValue))
default:
break
}
// Top right corner
switch corners {
case .all, .top, .topRight, .allButTopLeft, .allButBottomLeft, .allButBottomRight, .topRightBottomLeft:
cornerMasks.append(CACornerMask(rawValue: UIRectCorner.topRight.rawValue))
default:
break
}
// Bottom left corner
switch corners {
case .all, .bottom, .bottomLeft, .allButTopRight, .allButTopLeft, .allButBottomRight, .topRightBottomLeft:
cornerMasks.append(CACornerMask(rawValue: UIRectCorner.bottomLeft.rawValue))
default:
break
}
// Bottom right corner
switch corners {
case .all, .bottom, .bottomRight, .allButTopRight, .allButTopLeft, .allButBottomLeft, .topLeftBottomRight:
cornerMasks.append(CACornerMask(rawValue: UIRectCorner.bottomRight.rawValue))
default:
break
}
clipsToBounds = true
layer.cornerRadius = radius
layer.maskedCorners = CACornerMask(cornerMasks)
}
enum Corners {
case all
case top
case bottom
case topLeft
case topRight
case bottomLeft
case bottomRight
case allButTopLeft
case allButTopRight
case allButBottomLeft
case allButBottomRight
case left
case right
case topLeftBottomRight
case topRightBottomLeft
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment