如果您将使用SKTexture,您可以使用CGContext 绘制图表。
首先,您需要创建图表点数组,例如:
let pathPoints: [CGPoint] = [...]
然后您需要调用UIGraphicsBeginImageContext(rect.size),其中rect.size 是CGSize 对象,它决定了将在其中嵌入图表的区域的大小,rect 是CGRect 对象。下一步是创建CGContextRef 及其设置:
UIGraphicsBeginImageContext(size)
let context: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor);
CGContextSetLineWidth(context, lineWidth)
其中lineWidth 是CGFloat 类型值。
在此之后,您可以在这样的上下文中创建图表:
if pathPoints.count > 1 {
CGContextMoveToPoint(context, pathPoints.first!.x, pathPoints.first!.y)
for var i = 1; i < pathPoints.count; ++i {
CGContextAddLineToPoint(context, pathPoints[i].x, pathPoints[i].y)
}
}
CGContextStrokePath(context)
然后你需要像这样创建图像表单上下文:
let image = UIGraphicsGetImageFromCurrentImageContext()
然后创建纹理形式image 并将其用于将添加到场景中的SKSpriteNode 对象:
let pathTexture = SKTexture(image: image)
let pathNode = SKSpriteNode(texture: pathTexture)
pathNode.position = CGPointMake(rect.origin.x + rect.width/2 - lineWidth/2, rect.origin.y + rect.height/2 - lineWidth/2)
pathNode.zPosition = 0
someParrentNodeThatOnScene.addChild(pathNode)
UIGraphicsEndImageContext()
这就是创建图表所需的全部内容。