Converting between CACornerMask and UIRectCorner. avoid memorize the CACornerMask options.
CACornerMask
UIRectCorner
2
extension CACornerMask {
static let allCorners: CACornerMask = [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMaxXMaxYCorner]
}
init(_ rectCorner: UIRectCorner) { let corners = Self.from(rectCorner) self.init([corners]) } func toUIRectCorner() -> UIRectCorner { UIRectCorner(self) } static func from(_ rectCorner: UIRectCorner) -> CACornerMask { var caCorners = CACornerMask() if rectCorner.contains(.allCorners) { caCorners = .allCorners } else { if rectCorner.contains(.topRight) { caCorners.insert(.layerMaxXMinYCorner) } if rectCorner.contains(.topLeft) { caCorners.insert(.layerMinXMinYCorner) } if rectCorner.contains(.bottomRight) { caCorners.insert(.layerMaxXMaxYCorner) } if rectCorner.contains(.bottomLeft) { caCorners.insert(.layerMinXMaxYCorner) } } return caCorners }
extension UIRectCorner {
init(_ caCorners: CACornerMask) { let corners = Self.from(caCorners) self.init([corners]) } func toCACornerMask() -> CACornerMask { CACornerMask(self) } static func from(_ corners: CACornerMask) -> UIRectCorner { var rectCorners = UIRectCorner() if corners.contains(.layerMinXMinYCorner) { rectCorners.insert(.topLeft) } if corners.contains(.layerMaxXMinYCorner) { rectCorners.insert(.topRight) } if corners.contains(.layerMinXMaxYCorner) { rectCorners.insert(.bottomLeft) } if corners.contains(.layerMaxXMaxYCorner) { rectCorners.insert(.bottomRight) } return rectCorners }
convert UIRectCorner to CACornerMask
extension UIRectCorner { /// convert UIRectCorner to CACornerMask var caCornerMask: CACornerMask { var cornersMask = CACornerMask() if self.contains(.topLeft) { cornersMask.insert(.layerMinXMinYCorner) } if self.contains(.topRight) { cornersMask.insert(.layerMaxXMinYCorner) } if self.contains(.bottomLeft) { cornersMask.insert(.layerMinXMaxYCorner) } if self.contains(.bottomRight) { cornersMask.insert(.layerMaxXMaxYCorner) } return cornersMask } }
use like this
let rectCorner: UIRectCorner = [.topLeft, .bottomRight] let cornerMask = rectCorner.caCornerMask //result in cornerMask = [.layerMinXMinYCorner, .layerMaxXMaxYCorner]
Click here to cancel reply.
2
Answers
extension CACornerMask {
}
extension CACornerMask {
}
extension UIRectCorner {
}
convert UIRectCorner to CACornerMask
use like this