skip to Main Content

Converting between CACornerMask and UIRectCorner.
avoid memorize the CACornerMask options.

2

Answers


  1. Chosen as BEST ANSWER

    extension CACornerMask {

    static let allCorners: CACornerMask = [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMaxXMaxYCorner]
    

    }

    extension CACornerMask {

    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
    }
    

    }


  2. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search