【发布时间】:2015-07-04 09:34:39
【问题描述】:
我有两段代码,其中一段运行良好(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}>
并显示如下图片
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
产生以下形状
两个代码段都被赋予了相同的输入,并使用相同的方法来计算弧度等。据我所知,除了输出之外,两者之间的一切都是相同的。还值得注意的是,我将每条路径在绘制后转换为UIImages 和NSImages。请让我知道我是否应该发布我用来执行此操作的代码。
我还尝试使用 moveToPoint 将两段代码都放到正确的起始位置(在 iOS 上是不必要的,在 OS X 上没有解决问题)。
非常感谢您的帮助。
【问题讨论】:
-
可能找到了解决方案——也许 Cocoa 版本需要角度而不是弧度? See here。当我得到解决方案时会更新。
标签: ios macos cocoa core-graphics