0
我試圖繪製一個垂直翻轉其中心的圓。 (翻轉中心看起來相同,以便我可以直觀地查看)。但是,所畫的位置是完全錯誤的。我想到的是,填充有在描邊區域 在drawrect方法內翻轉CGContext繪製錯誤的位置
circleRect = CGRect(x:100, y:100, width:100, height: 100)
override func draw(_ dirtyRect: CGRect) {
let ctx = NSGraphicsContext.current()!.cgContext
if(drawCircle) {
let path = getOvalPath(circleRect) //returns a oval cgpath for the rect, here the rect is a square so it gives a circle
//stroked without flipping
ctx.addPath(path)
ctx.setStrokeColor(CGColor.black)
ctx.strokePath()
//filled with flipping
ctx.saveGState
ctx.translateBy(x: 0, y: circleRect.size.height)
ctx.scaleBy(x: 1, y: -1)
ctx.addPath(path)
ctx.setFillColor(fillColor)
ctx.fillPath()
ctx.restoreGState()
}
drawCircle = true
setNeedsDisplay(circleRect)
哪一個更好,改變上下文或路徑?例如,如果我想繪製一個旋轉了一定程度的形狀,那麼我可以在形狀路徑上進行旋轉變換,或者在繪製時旋轉上下文以實現它。 –
無論哪一個讓你的代碼更簡單更好。這取決於情況。 –