在CACornerMask和UIRectCorner之间转换。避免记住CACornerMask选项。
CACornerMask
UIRectCorner
cgvd09ve1#
扩展CA角掩码{
static let allCorners: CACornerMask = [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMaxXMaxYCorner]
}扩展CA角掩码{
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 }
}扩展名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 }
}
afdcj2ne2#
将UIRectCorner转换为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 } }
像这样使用
let rectCorner: UIRectCorner = [.topLeft, .bottomRight] let cornerMask = rectCorner.caCornerMask //result in cornerMask = [.layerMinXMinYCorner, .layerMaxXMaxYCorner]
2条答案
按热度按时间cgvd09ve1#
扩展CA角掩码{
}
扩展CA角掩码{
}
扩展名UIRectCorner {
}
afdcj2ne2#
将UIRectCorner转换为CACornerMask
像这样使用