Как получить гладкий круг с CGContext, когда масштаб больше, используйте CALayer в NSScrollView - PullRequest
0 голосов
/ 25 мая 2018

см. Код:

ContentView для NSScrollView:

class ContentView: NSView {

    var documentLayer = DocumentLayer()

    override var isFlipped: Bool {
        return true
    }

    override func awakeFromNib() {
        wantsLayer = true
        layerContentsRedrawPolicy = .onSetNeedsDisplay
        documentLayer.backgroundColor = NSColor.darkGray.cgColor

        let shape = CAShapeLayer()
        let circlePath = CGMutablePath()
        circlePath.addEllipse(in: CGRect(x: 160, y: 100, width: 50, height: 50))
        shape.lineWidth = 1
        shape.strokeColor = NSColor.red.cgColor
        shape.fillColor = NSColor.clear.cgColor
        shape.path = circlePath
        documentLayer.addSublayer(shape)
        needsDisplay = true

    }

    override func makeBackingLayer() -> CALayer {
        return documentLayer
    }

   .....
}

DocumentLayer для ContentView:

class DocumentLayer : CALayer {

    override init() {
        super.init()
        contentsScale = 2
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(in ctx: CGContext) {
        super.draw(in: ctx)
        ctx.setStrokeColor(NSColor.red.cgColor)
        ctx.addEllipse(in: CGRect(x: 100, y: 100, width: 50, height: 50))
        ctx.drawPath(using: .stroke)
    }
}

Когда масштаб больше, получается ровный круг и негладкийcircle:

circle pic

Гладкий круг создается CAShapeLayer, а негладкий круг создается cgContext.

?

Как внутренняя работа CAShapeLayer?Как я могу получить сглаживание с CGContext?Это мои вопросы!

...