从点数组绘制平滑曲线 swift

draw smooth curve from array of points swift

我正在 Xcode 中使用 swift 和 sprite-kit 开发一个应用程序,我有一个排列在椭圆中的点数组。我想知道如何绘制连接数组点的平滑白色曲线。我很确定它与 SKShapeNode 有关系,但我不确定如何去做。在此先感谢您的帮助。

如果你想要一个完美的椭圆,你可以使用:

convenience init(ellipseInRect rect: CGRect)

convenience init(ellipseOfSize size: CGSize)

如果您只需要使用这些点,您可以创建一个 CGPath,并创建一个 SKShapeNode:

convenience init(path path: CGPath!)

convenience init(path path: CGPath!, centered centered: Bool)

可以在这些链接中找到更多信息:

CGPath: https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGPath/index.html

和 SKShapeNode:https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKShapeNode_Ref/index.html#//apple_ref/occ/clm/SKShapeNode/shapeNodeWithPath:centered

最后是 4 个动作:

let rect = CGRect(x: 10, y: 10, width: 30, height: 40)
let size = rect.size
//for convenience init(ellipseInRect rect: CGRect)
let ellipse1 = SKShapeNode(ellipseInRect: rect)
//for convenience init(ellipseOfSize size: CGSize)
let ellipse2 = SKShapeNode(ellipseOfSize: size)

//path is a little harder to explain without more context
let path = CGPathCreateWithRect(rect, nil)
//for convenience init(path path: CGPath!)
let ellipse3 = SKShapeNode(path: path)
//for convenience init(path path: CGPath!, centered centered: Bool)
let ellipse4 = SKShapeNode(path: path, centered: true)