Setup cornerRadius in a specific direction

ios dev·2021년 9월 15일
0
extension UIView {
    // MARK: - Setup Radius
    enum RadiusEdge {
        case all
        case topLeft
        case bottomLeft
        case bottomRight
        case topRight
    }
    
    func setupCornerRadius(radius: CGFloat, edges: [RadiusEdge] = [.all]) {
        var maskedCorners = CACornerMask()
        for edge in edges {
            switch edge {
            case .all: maskedCorners = [.layerMaxXMaxYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
            case .topLeft: maskedCorners.insert(.layerMinXMinYCorner)
            case .bottomLeft: maskedCorners.insert(.layerMinXMaxYCorner)
            case .bottomRight: maskedCorners.insert(.layerMaxXMaxYCorner)
            case .topRight: maskedCorners.insert(.layerMaxXMinYCorner)
            }
        }
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
        self.layer.maskedCorners = maskedCorners
    }
}

0개의 댓글