Swift 在NSScrollView中使用CALayer时,如何使用CGContext获得平滑圆
参见代码: NSScrollView的ContentView:Swift 在NSScrollView中使用CALayer时,如何使用CGContext获得平滑圆,swift,macos,cgcontext,smoothing,Swift,Macos,Cgcontext,Smoothing,参见代码: NSScrollView的ContentView: class ContentView: NSView { var documentLayer = DocumentLayer() override var isFlipped: Bool { return true } override func awakeFromNib() { wantsLayer = true layerContentsRedr
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
}
.....
}
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)
}
}
当比例变大时,则得到平滑圆和不平滑圆:
平滑圆由CAShapeLayer创建,不平滑圆由cgContext创建
有人注意到我吗?