NSBezierPath(roundedRect rect: CGRect, byRoundingCorners corners: NSRectCorner, cornerRadii: NSSi...

public struct NSRectCorner: OptionSet {

    public let rawValue: Int

    public static let topLeft = NSRectCorner(rawValue: 1<<0)

    public static let topRight = NSRectCorner(rawValue: 1<<1)

    public static let bottomLeft = NSRectCorner(rawValue: 1<<2)

    public static let bottomRight = NSRectCorner(rawValue: 1<<3)

    public static let allCorners = NSRectCorner(rawValue: 1111<<0)

    

    public init(rawValue: Int) {

            self.rawValue = rawValue

        }

}


public extension NSBezierPath {

    var cgPath: CGPath {

        let path = CGMutablePath()

        var points = [CGPoint](repeating: .zero, count: 3)

        for i in 0 ..< self.elementCount {

            let type = self.element(at: i, associatedPoints: &points)

            switch type {

            case .moveToBezierPathElement:

                path.move(to: points[0])

            case .lineToBezierPathElement:

                path.addLine(to: points[0])

            case .curveToBezierPathElement:

                path.addCurve(to: points[2], control1: points[0], control2: points[1])

            case .closePathBezierPathElement:

                path.closeSubpath()

            }

        }

        return path

    }

    

    static func create(roundedRect rect: CGRect, byRoundingCorners corners: NSRectCorner, cornerRadii: NSSize) -> NSBezierPath {

        let minX = rect.minX

        let minY = rect.minY

        let maxX = rect.maxX

        let maxY = rect.maxY

        let radius = cornerRadii.width

        

        let center1 = NSPoint(x: maxX - radius, y: minY + radius)

        let center2 = NSPoint(x: maxX - radius, y: maxY - radius )

        let center3 = NSPoint(x: minX + radius , y: maxY - radius)

        let center4 = NSPoint(x: minX + radius, y: minY + radius )

        

        let halfPath = NSBezierPath.init()

        halfPath.move(to: NSPoint(x: maxX - radius, y: minY))

        

        if ((corners.rawValue & NSRectCorner.bottomRight.rawValue) != 0) {

            /// 右下圆角

            halfPath.appendArc(withCenter: center1, radius: radius, startAngle: 270, endAngle: 360, clockwise: false)

            halfPath.line(to: NSPoint(x: maxX , y: maxY - radius ))

        }else {

            halfPath.line(to: NSPoint(x: maxX , y: minY ))

            halfPath.line(to: NSPoint(x: maxX , y: maxY - radius ))

        }

        

        if ((corners.rawValue & NSRectCorner.topRight.rawValue) != 0) {

            /// 右上圆角

            halfPath.appendArc(withCenter: center2, radius: radius, startAngle: 0, endAngle: 90, clockwise: false)

            halfPath.line(to: NSPoint(x: minX + radius , y:  maxY ))

        }else {

            halfPath.line(to: NSPoint(x: maxX , y:  maxY ))

            halfPath.line(to: NSPoint(x: minX + radius , y:  maxY ))

        }

        

        if ((corners.rawValue & NSRectCorner.topLeft.rawValue) != 0) {

            /// 左上圆角

            halfPath.appendArc(withCenter: center3, radius: radius, startAngle: 90, endAngle: 180, clockwise: false)

            halfPath.line(to: NSPoint(x: minX , y: minY + radius ))

        }else {

            halfPath.line(to: NSPoint(x: minX , y: maxY ))

            halfPath.line(to: NSPoint(x: minX , y: minY + radius ))

        }

        

        if ((corners.rawValue & NSRectCorner.bottomLeft.rawValue) != 0) {

            /// 左下圆角

            halfPath.appendArc(withCenter: center4, radius: radius, startAngle: 180, endAngle: 270, clockwise: false)

            halfPath.line(to: NSPoint(x: maxX - radius , y: minY ))

        }else {

            halfPath.line(to: NSPoint(x: minX , y: minY ))

            halfPath.line(to: NSPoint(x: maxX - radius , y: minY ))

        }

        

        halfPath.close()

        return halfPath

    }

    

}

你可能感兴趣的:(NSBezierPath(roundedRect rect: CGRect, byRoundingCorners corners: NSRectCorner, cornerRadii: NSSi...)