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)
どちらが良いですか、コンテキストまたはパスを変換しますか?たとえば、ある角度だけ回転した図形を描画したい場合は、図形のパスを回転変換するか、図形描画中に回転させて回転させることができます。 –
あなたのコードをシンプルにする方が良いでしょう。それは状況によって異なります。 –