ios - UIBezierPath 和 NSBezierPath 在绘制圆弧时产生不同的结果

标签 ios macos cocoa core-graphics

我有两段代码,其中一段运行良好(iOS 版本),另一段运行不正常(Mac 版本)。我无法弄清楚使用 NSBezierPath 而不是 UIBezierPath 的 Mac 版本有什么问题。

iOS 版本

我有一段代码可以使用 UIBezierPath 在 iOS 上正常运行。这是代码:

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。请让我知道是否应该发布我用来执行此操作的代码。

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

非常感谢您的帮助。

最佳答案

事实证明,在 iOS 上 addArcWithCenterstartAngleendAngle 取弧度,而在 OS X 上 appendBezierPathWithArcWithCenter 取弧度学位。

Here's the link to the documentation for OS X .

关于ios - UIBezierPath 和 NSBezierPath 在绘制圆弧时产生不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31219122/

相关文章:

ios - NSData 只有类方法 "NSData.dataWithContentsOfMappedFile(<#path: String#>)"

objective-c - OSX cocoa 应用程序 : Which method for rescaling an image is best with respect to image quality?

ios - 在 SwiftUI 中监听 AVPlayer timeControlStatus 的变化

ios - Realm 迁移,初始化位置

iphone - 管理 iphone 项目中的构建目标变量

objective-c - 正确处理 NSURLConnection 错误

cocoa - 在绑定(bind)到 NSArrayController 的 NSTableView 中显示行索引

ios - 重新加载 UICollectionView 部分并保留滚动位置

iphone - 使 UICollectionView 中的 insertItemsAtIndexPath 不设置动画

macos - 我可以知道 NSTextField 文本部分的确切矩形吗?