如何使用 Swift 从 UIBezierPath 创建虚线 SKShapeNode?
How Can I Create a Dashed Line SKShapeNode from a UIBezierPath, Using Swift?
我找到的所有解释似乎都在说同一件事。我不明白为什么这不起作用。
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))
我找到的所有解释似乎都在说同一件事。我不明白为什么这不起作用。
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))