我发现的所有解释似乎都说相同的话。我不知道为什么这不起作用。

var linePath = UIBezierPath()
linePath.move(to: CGPoint(x: 50, y: 50))
linePath.addLine(to: CGPoint(x: 100, y: 100))

var pattern : [CGFloat] = [10.0, 10.0]
linePath.setLineDash(pattern, count: pattern.count, phase: 0)
linePath.lineWidth = 10
linePath.lineCapStyle = .round

let shape = SKShapeNode()
shape.path = linePath.cgPath
shape.strokeColor = UIColor.white

self.addChild(shape)

这段代码成功地画了一条线,但是shape不继承linePath的虚线属性,包括宽度。有任何想法吗?

最佳答案

let linePath = UIBezierPath()
linePath.move(to: CGPoint(x: 50, y: 50))
linePath.addLine(to: CGPoint(x: 100, y: 100))

var pattern: [CGFloat] = [10.0, 10.0]
let dashed = CGPathCreateCopyByDashingPath (linePath.CGPath, nil, 0, pattern, 2)

var shape = SKShapeNode(path: dashed)
shape.strokeColor = UIColor.white

self.addChild(shape)

注意:在Swift 3中,CGPathCreateCopyByDashingPath已替换为path.copy(dashingWithPhase:lengths:)
例如
let dashed = SKShapeNode(path: linePath.cgPath.copy(dashingWithPhase: 2, lengths: pattern))

10-08 06:09