Skip to content

Instantly share code, notes, and snippets.

@marlonjames71
Created April 26, 2021 17:20
Show Gist options
  • Select an option

  • Save marlonjames71/09d65599fb0cf94584bdace2f63d50cd to your computer and use it in GitHub Desktop.

Select an option

Save marlonjames71/09d65599fb0cf94584bdace2f63d50cd to your computer and use it in GitHub Desktop.
Can specify which corners of a `UIView` are rounded. Extended `CACornerMask` OptionSet to be a bit more readable and friendlier to use.
extension UIView {
/// Applies rounded corners with masking to specific corners and with corner curve option.
func roundCorners(cornerRadius: CGFloat, roundedStyle: CALayerCornerCurve, corners: CACornerMask) {
layer.cornerRadius = cornerRadius
layer.cornerCurve = roundedStyle
layer.maskedCorners = corners
}
}
extension CACornerMask {
/// Applies mask to all corners
public static var allCorners: CACornerMask {
[.layerMaxXMaxYCorner, .layerMinXMinYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner]
}
/// Applies mask to top left and top right corners
public static var topCorners: CACornerMask {
[.layerMinXMinYCorner, .layerMaxXMinYCorner]
}
/// Applies mask to bottom left and bottom right corners
public static var bottomCorners: CACornerMask {
[.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
}
/// Applies mask to top left and bottom left corners
public static var leftCorners: CACornerMask {
[.layerMinXMinYCorner, .layerMinXMaxYCorner]
}
/// Applies mask to top right and bottom right corners
public static var rightCorners: CACornerMask {
[.layerMaxXMinYCorner, .layerMaxXMaxYCorner]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment