【发布时间】:2018-11-24 04:55:19
【问题描述】:
- 我使用 UIBezierPath 创建一个路径,然后从该路径创建一个 SKShapeNode。
- 我希望用户通过触摸屏幕来绘制相同的路径以匹配我的原始路径。
这是代码:
导入 SpriteKit 导入 GameplayKit
类 GameScene:SKScene {
var orginalPath: UIBezierPath!
var orgialShape: SKShapeNode!
var userTouchs = [CGPoint]()
var drawingLine: SKShapeNode!
override func didMove(to view: SKView) {
orginalPath = UIBezierPath()
orginalPath.move(to: CGPoint(x: 170, y: 230))
orginalPath.addLine(to: CGPoint(x: 370, y: 230))
orginalPath.addQuadCurve(to: CGPoint(x: 480, y: 55), controlPoint: CGPoint(x: 525, y: 150))
orgialShape = SKShapeNode(path: orginalPath.cgPath)
orgialShape.strokeColor = UIColor.white
orgialShape.lineWidth = 30
orgialShape.lineCap = .round
orgialShape.lineJoin = .round
orgialShape.zPosition = 0
orgialShape.name = "orginal"
addChild(orgialShape)
}
func createLine() {
if userTouchs.count > 2 {
let line = SKShapeNode(points: &userTouchs, count: userTouchs.count)
line.strokeColor = UIColor.red
line.lineWidth = 10
line.lineCap = .round
line.zPosition = 10
line.isAntialiased = true
line.name = "newLine"
addChild(line)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let objects = nodes(at: location)
for object in objects {
if object.name == "orginal" {
userTouchs.append(location)
}
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let objects = nodes(at: location)
for object in objects {
if object.name == "orginal" {
print(location)
userTouchs.append(location)
createLine()
}
}
}
}
这段代码应该让用户“只”在我的路径上绘制,就像我在图片中显示的那样:
但问题是,当我编写代码时,我发现我可以在路径外画一条线,实际上它看起来像一个正方形,而不仅仅是我在图片中显示的路径:
所以请任何人向我解释让用户只在我的路径上绘制的正确方法,如果他触摸了路径,他就无法绘制任何东西。
【问题讨论】:
标签: ios sprite-kit uibezierpath skshapenode