ios - 在 Sprite Kit 中检测长触摸

标签 ios swift sprite-kit

我在 SKScene 中有一个节点,我根据用户的触摸移动它。基本上,这个角色也应该试图跟随用户的手指(假设手指在屏幕上)。我目前已经这样实现了,效果很好:

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    player.runAction(SKAction.moveTo(touch.locationInNode(self), duration: 1))
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    player.runAction(SKAction.moveTo(touch.locationInNode(self), duration: 1))
}

override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
    player.removeAllActions()
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    player.removeAllActions()
}

但是,问题是如果用户将他/她的手指放在手机上。 touchesBegan 只被调用一次,那是在点击开始的时候,而不是在它被按住的时候。我希望玩家角色不断地尝试伸手去拿手指。

我将相机放在节点的中心,因此节点应该触摸手指的唯一时间是用户将手指放在节点上/节点中(即与节点相同的位置)。因此,在我运行 SKAction 移动节点后,触摸无效,因为它位于旧位置。

我该怎么做?

最佳答案

你可以像这样注册一个长触摸事件:

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
self.view.addGestureRecognizer(longPressRecognizer)

func longPressed(sender: UILongPressGestureRecognizer) {
    // your code
}

关于ios - 在 Sprite Kit 中检测长触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30337608/

相关文章:

ios - fatal error : Index out of range - Swift 3

ios - 在哪里使用 CGPathRelease?

swift - 如何构建用于拖放拼图 block 的对齐网格功能?

ios - setStatusBarHidden 已弃用,但唯一有效的方法

ios - Swift 4 - 如何构建 json 对象并在 switch 中使用可解码(不起作用)

ios - uiview 的高度不正确

ios - 除非选择为目标,否则框架中的 Xcode 警告不会显示在项目导航器中

objective-c - 为 OS X 实现 float 按钮 UI View 的最简单方法是什么

swift - 在 Sprite 套件上写入字符串

ios - 如何访问 NSHTTPURLResponse 的 "Content-Type" header ?