I fount this solution but I can't make it into swift code
这是我尝试的
var pattern[2]:CGFloat; this
var dashed: CGPathRef = CGPathCreateCopyByDashingPath(CGPathCreateCopyByDashingPath(path, transform, phase, lengths, count);
var myShapeNode: SKShapeNode!;
var CGPathCreateCopyByDashingPath:CGPathRef;
答案 0 :(得分:11)
这是你在swift中绘制虚线的方法。您可以根据需要更改参数。
let bezierPath = UIBezierPath()
let startPoint = CGPointMake(0, 250)
let endPoint = CGPointMake(450, 250)
bezierPath.moveToPoint(startPoint)
bezierPath.addLineToPoint(endPoint)
var pattern : [CGFloat] = [10.0, 10.0]
let dashed = CGPathCreateCopyByDashingPath (bezierPath.CGPath, nil, 0, pattern, 2)
var shapeNode = SKShapeNode(path: dashed)
shapeNode.position = CGPointMake(100, 100)
self.addChild(shapeNode)
答案 1 :(得分:2)
对于任何想要研究如何将这个相同的主体应用于SKShapeNode的人来说,这是一个例子。带有蓝色虚线的矩形。
import SpriteKit
import GameplayKit
let square = SKShapeNode(rectOfSize: CGSize(width: 64, height: 64))
let circle = SKShapeNode(circleOfRadius: 20.0)
var pattern : [CGFloat] = [2.0, 2.0]
let dashed = CGPathCreateCopyByDashingPath (square.path, nil, 0, pattern, 2)
var shapeNode = SKShapeNode(path: dashed!)
shapeNode.fillColor = UIColor.blueColor()
shapeNode.strokeColor = UIColor.blueColor()
答案 2 :(得分:2)
快速4:
let square = SKShapeNode(rectOf: CGSize(width: 64, height: 64))
let pattern : [CGFloat] = [4.0, 4.0]
let dashed = square.path?.copy(dashingWithPhase: 1, lengths: pattern)
let shapeNode = SKShapeNode(path: dashed!)
shapeNode.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
shapeNode.fillColor = SKColor.clear
shapeNode.strokeColor = SKColor.red
shapeNode.lineWidth = 2
self.addChild(shapeNode)
答案 3 :(得分:1)
雨燕5
if let path = path?.copy(dashingWithPhase: 1, lengths: [5, 5]) {
let line = SKShapeNode(path: path)
line.strokeColor = .white
self.addChild(line)
}
答案 4 :(得分:0)
除了上述Mike Glukhov提供的解决方案之外,这是我的解决方案:
func drawTrace(pointArray: Array<CGPoint>) {
if pointArray.count > 1 {
let pattern : [CGFloat] = [10.0, 10.0]
let path = CGMutablePath.init()
// start at the first point.
path.move(to: pointArray[0])
// now add all of the others.
for p in 1 ..< pointArray.count {
path.addLine(to: pointArray[p])
}
// create the dashed path.
let dashedPath = path.copy(dashingWithPhase: 1, lengths: pattern)
let dashName = "dash"
// now create the node
let line = SKShapeNode(path: dashedPath)
line.strokeColor = .white
line.name = dashName
if let parent = self.parent {
if let oldLine = parent.childNode(withName: dashName) {
oldLine.removeFromParent()
}
parent.addChild(line)
}
}
}