[英]How Can I Create a Dashed Line SKShapeNode from a UIBezierPath, Using Swift?
All of the explanations I've found seem to say the same thing. 我发现的所有解释似乎都说相同的话。 I can't figure out why this isn't working. 我不知道为什么这不起作用。
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)
This code successfully draws a line but shape
does not inherit the dashed properties of linePath
, including even the width. 这段代码成功绘制了一条线,但是shape
不继承linePath
的虚线属性,包括宽度。 Any ideas? 有任何想法吗?
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)
NOTE: In Swift 3 CGPathCreateCopyByDashingPath
has been replaced by path.copy(dashingWithPhase:lengths:)
注意:在Swift 3中, CGPathCreateCopyByDashingPath
已由path.copy(dashingWithPhase:lengths:)
取代。
eg 例如
let dashed = SKShapeNode(path: linePath.cgPath.copy(dashingWithPhase: 2, lengths: pattern))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.