我正在尝试实现一个重写的 TouchMoved 函数,以使 SKSpriteNode 能够由用户移动。但是,我必须非常缓慢地移动节点,以便它在拖动时跟随我的触摸。此外,后台还有三个 SKSpriteNode(您将看到我明确将其设置为 .userInteractionEnabled = false),这些节点偶尔会响应触摸。任何帮助将不胜感激。如果您需要代码的任何其他部分,请告诉我。
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
var positionInScene = CGPoint(x: 375.0, y: 400.0) //sets a default position
titleLabel.userInteractionEnabled = false
drawingBoard.userInteractionEnabled = false
sideBar.userInteractionEnabled = false
for touch in touches {
positionInScene = touch.locationInNode(self)
if self.nodeAtPoint(positionInScene) is SKSpriteNode {
if (self.nodeAtPoint(positionInScene)).name == movableNodeName { //movableNodeName is the name assigned to all SKSpriteNodes that should be draggable
//I know this might be a strange way of doing it
(self.nodeAtPoint(touch.previousLocationInNode(self))).position = positionInScene
}
}
}
}
最佳答案
我已经通过仅设置节点的坐标来解决此问题,该节点在移动时不应移回原来的位置。这似乎有效,因为我无法在测试中再次复制该错误。如果有人能提出更好的解决方案,我很乐意听到。
关于swift - TouchMoved 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34959410/