填充从 CGPath 创建的 SKShapeNode
Fill SKShapeNode created from CGPath
我正在尝试创建一个基于点数组的自定义 SKShapeNode。
这些点形成一个封闭的形状,最终需要填充这个形状。
这就是我到目前为止的想法,但由于某种原因笔划画得很好,但形状仍然是空的。我错过了什么?
override func didMoveToView(view: SKView)
{
let center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, center.x, center.y)
CGPathAddLineToPoint(path, nil, center.x + 50, center.y + 50)
CGPathMoveToPoint(path, nil, center.x + 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y + 50)
CGPathMoveToPoint(path, nil, center.x - 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y - 50)
CGPathMoveToPoint(path, nil, center.x - 50, center.y - 50)
CGPathAddLineToPoint(path, nil, center.x, center.y)
CGPathCloseSubpath(path)
let shape = SKShapeNode(path: path)
shape.strokeColor = SKColor.blueColor()
shape.fillColor = SKColor.redColor()
self.addChild(shape)
}
您的 path
出了点问题。您通常会调用 CGPathMoveToPoint
来设置路径的起点,然后调用一系列 CGPathAdd*
来将线段添加到路径中。尝试像这样创建它:
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, center.x, center.y)
CGPathAddLineToPoint(path, nil, center.x + 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y - 50)
CGPathCloseSubpath(path)
阅读 CGPath Reference(搜索 CGPathMoveToPoint
)了解更多详情。
例如你不需要为这个动作使用CGPath,你可以做这样的事情:
let points: [CGPoint] = [CGPointMake(center.x, center.y), ...] // All your points
var context: CGContextRef = UIGraphicsGetCurrentContext()
CGContextAddLines(context, points, UInt(points.count))
CGContextSetFillColorWithColor(context, UIColor.redColor().CGColor)
CGContextFillPath(context)
let shape = SKShapeNode(path: CGContextCopyPath(context))
...
我正在尝试创建一个基于点数组的自定义 SKShapeNode。 这些点形成一个封闭的形状,最终需要填充这个形状。
这就是我到目前为止的想法,但由于某种原因笔划画得很好,但形状仍然是空的。我错过了什么?
override func didMoveToView(view: SKView)
{
let center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, center.x, center.y)
CGPathAddLineToPoint(path, nil, center.x + 50, center.y + 50)
CGPathMoveToPoint(path, nil, center.x + 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y + 50)
CGPathMoveToPoint(path, nil, center.x - 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y - 50)
CGPathMoveToPoint(path, nil, center.x - 50, center.y - 50)
CGPathAddLineToPoint(path, nil, center.x, center.y)
CGPathCloseSubpath(path)
let shape = SKShapeNode(path: path)
shape.strokeColor = SKColor.blueColor()
shape.fillColor = SKColor.redColor()
self.addChild(shape)
}
您的 path
出了点问题。您通常会调用 CGPathMoveToPoint
来设置路径的起点,然后调用一系列 CGPathAdd*
来将线段添加到路径中。尝试像这样创建它:
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, center.x, center.y)
CGPathAddLineToPoint(path, nil, center.x + 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y + 50)
CGPathAddLineToPoint(path, nil, center.x - 50, center.y - 50)
CGPathCloseSubpath(path)
阅读 CGPath Reference(搜索 CGPathMoveToPoint
)了解更多详情。
例如你不需要为这个动作使用CGPath,你可以做这样的事情:
let points: [CGPoint] = [CGPointMake(center.x, center.y), ...] // All your points
var context: CGContextRef = UIGraphicsGetCurrentContext()
CGContextAddLines(context, points, UInt(points.count))
CGContextSetFillColorWithColor(context, UIColor.redColor().CGColor)
CGContextFillPath(context)
let shape = SKShapeNode(path: CGContextCopyPath(context))
...