绘制弧时,UIBezierPath和NSBezierPath会产生不同的结果

时间:2015-07-04 09:34:39

标签: ios macos cocoa core-graphics

我有两段代码,其中一段完美无缺(iOS版本),另一段无法正常运行(Mac版本)。我无法解决使用NSBezierPath而不是UIBezierPath的Mac版本的问题。

iOS版

我有一段代码完全像我在iOS上使用UIBezierPath那样工作。这是代码:

let center = CGPoint(x: size.width / 2.0, y: size.height / 2.0)
let radius = size.width / 2.0
let startAngle = angle
let endAngle = 180.0 - startAngle

let bezierPath = UIBezierPath()
bezierPath.addArcWithCenter(center, radius: radius, startAngle: startAngle.radians, endAngle: endAngle.radians, clockwise: true)
println(bezierPath)
return bezierPath

它产生以下输出

<UIBezierPath: 0x7fe348f82910; <MoveTo {42.677669529663689, 42.677669529663689}>,
<CurveTo {7.3223304703363148, 42.677669529663689} {32.914562235881945, 52.440776823445439} {17.085437764118062, 52.440776823445432}>

并显示以下图片

correct shape

Mac OS X版

这正是我想要的,除了它适用于iOS。我想将此代码翻译为OS X / Cocoa。这就是我提出的问题

let center = CGPoint(x: size.width / 2.0, y: size.height / 2.0)
let radius = size.width / 2.0
let startAngle = angle
let endAngle = 180.0 - startAngle

let bezierPath = NSBezierPath()
bezierPath.appendBezierPathWithArcWithCenter(center, radius: radius, startAngle: startAngle.radians, endAngle: endAngle.radians, clockwise: true)
println(bezierPath)
return bezierPath

(它与iOS代码几乎相同)。

这里的输出是

Path <0x600000122800>
Bounds: {{-4.9018102839097546e-05, -4.9018102839077596e-05}, {50.000098036205685, 50.000094758067902}}
Control point bounds: {{-0.18691031775632938, -0.18691031775632855}, {50.373820635512658, 50.37016203886877}}
49.997651 25.342684 moveto
50.186910 11.536862 39.148505 0.191608 25.342684 0.002349 curveto
11.536862 -0.186910 0.191608 10.851495 0.002349 24.657316 curveto
-0.186910 38.463138 10.851495 49.808392 24.657316 49.997651 curveto
38.196255 50.183252 49.422202 39.556558 49.978864 26.027794 curveto

产生以下形状

incorrect result

两个代码段都被赋予相同的输入并使用相同的方法来计算弧度等。据我所知,除了输出之外,两者之间的一切都是相同的。值得注意的是,在绘制之后,我将每个路径转换为UIImageNSImage s。如果我发布了我用来执行此操作的代码,请告诉我。

我还尝试使用moveToPoint将两段代码放到正确的起始位置(在iOS上没有必要,没有解决OS X上的问题)。

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:7)

原来,在iOS上addArcWithCenter需要startAngleendAngle的弧度,而OS X appendBezierPathWithArcWithCenter需要度数。

Here's the link to the documentation for OS X