Last active
January 10, 2019 08:22
-
-
Save henrik-dmg/eae10abe8ca12041cbd63d3d399e2ce9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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