swift - TouchMoved 无法正常工作

标签 swift

我正在尝试实现一个重写的 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/

相关文章:

swift - 将运行脚本生成的文件添加到Xcode中的测试目标编译列表中

ios - 一对一关系的值类型 Not Acceptable : property = "user"; desired type = User; given type = User;

ios - 什么时候调用协议(protocol)函数?

ios - 运行 Stanford 的 CS193p Smashtag 应用程序时出现 _BSMachError

swift - 避免 iOS 上的 Firebase/BoringSSL 控制台日志

ios - Swift - 是否有 bool 可转换协议(protocol)或任何其他方式来重载转换为自定义类型?

ios - swift 3 : How to mute/unmute microphone during video recording using AVFoundation

ios - 将 UIView 输出为视频流

ios - 如何在 Swift 中使用 Codable 和 URLSession.shared.uploadTask (multipart/form-data) 上传图像文件?

ios - Swift 3 测试 SKNode 的 SKSpriteNode 子类是否包含触摸