我已经实现了单击和双击。但每当我双击屏幕时,也会运行单击代码。请参阅以下代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first as UITouch!
let touchLocation = touch?.location(in: self)
let touchedNode = self.atPoint(touchLocation!)
if touch?.tapCount == 1 {
print("single tap")
if let name = touchedNode.name {
if name == "enemySprite"
{
//handle single Tap
}
}
}
if touch?.tapCount == 2 {
print("double tap")
if let name = touchedNode.name {
if name == "enemySprite"
{
//handle double Tap
}
}
}
}
如何使每个分接计数互斥?
更新
根据Apurv的回答和Whirlwind的评论,我认为以下代码显示有效。
override func didMove(to view: SKView) {
var tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.view.sampleTapGestureTapped(recognizerMethod:)))
self.view.addGestureRecognizer(tapGesture!)
tapGesture.numberOfTapsRequired = 1;
var tapGestureDouble = UITapGestureRecognizer(target: self, action: #selector(self.view.sampleTapGestureTapped(recognizerMethod2:)))
self.view.addGestureRecognizer(tapGestureDouble!)
tapGestureDouble.numberOfTapsRequired = 2;
[tapGesture requireGestureRecognizerToFail : tapGestureDouble];
}
它不起作用,我说错误,
Value of type ’SKView?’ has no member ‘sampleTapGestureTapped(recognizerMethod:)’ and
Value of type ’SKView?’ has no member ‘sampleTapGestureTapped(recognizerMethod2:)’
指向点击手势变量的声明
另外,我收到了错误
Expected expression in the container literal
指向上述方法的最后一行。
我想我在这里错过了一些东西,我真的很感激对此的一些帮助和澄清。
答案 0 :(得分:4)
您不必访问ViewController ...您可以在场景中处理点击,如下所示:
import SpriteKit
class GameScene: SKScene {
var recognizers:[UIGestureRecognizer] = []
override func didMove(to view: SKView) {
// Get label node from scene and store it for use later
let single = UITapGestureRecognizer(target: self, action: #selector(GameScene.singleTapHandler(recognizer:)))
view.addGestureRecognizer(single)
single.numberOfTapsRequired = 1
recognizers.append(single)
let double = UITapGestureRecognizer(target: self, action: #selector(GameScene.doubleTapHandler(recognizer:)))
view.addGestureRecognizer(double)
double.numberOfTapsRequired = 2
recognizers.append(double)
single.require(toFail: double)
}
override func willMove(from view: SKView) {
super.willMove(from: view)
for recognizer in recognizers {
self.view?.removeGestureRecognizer(recognizer)
}
recognizers.removeAll()
}
func singleTapHandler(recognizer:UITapGestureRecognizer){
print("Single tap detected")
}
func doubleTapHandler(recognizer:UITapGestureRecognizer){
print("Double tap detected")
}
}
当然,在ViewController
上调用一些方法可以通过几种方式完成(例如委托)......但是,根据你的评论,我认为处理当前场景中的点击是你想要的实现。
修改强>
要回答评论中的问题...您可以通过在视图中查找触摸位置,将其转换为场景中的位置以及抓取该节点来抓取被触摸的节点位置:
func singleTapHandler(recognizer:UITapGestureRecognizer){
print("Single tap detected")
let locationInView = recognizer.location(in: self.view)
let locationInScene = self.convertPoint(fromView: locationInView)
print(locationInScene)
let node = atPoint(locationInScene)
if let nodeName = node.name {
//do stuff
}
//or
if let sprite = node as? SKSpriteNode {
// node is an SKSpriteNode
}
}
答案 1 :(得分:3)
处理点按的更好方法是使用手势识别器。使用UITapGestureRecognizer
并设置要跟踪的正确数量的点按。
现在允许两个分接头无缝地添加代码,以确保只有在双击失败时才会调用单个手势。请参阅下面的代码。
var tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.sampleTapGestureTapped(recognizerMethod:)))
self.view.addGestureRecognizer(tapGesture!)
tapGesture.numberOfTapsRequired = 1;
var tapGestureDouble = UITapGestureRecognizer(target: self, action: #selector(ViewController.sampleTapGestureTapped(recognizerMethod2:)))
self.view.addGestureRecognizer(tapGestureDouble!)
tapGestureDouble.numberOfTapsRequired = 2;
[tapGesture requireGestureRecognizerToFail:tapGestureDouble];