I have implemented both single and double tap. But whenever I double tap the screen the single tap code is also run. Please see code below:
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
}
}
}
}
How can I make each of the tap counts mutually exclusive?
UPDATE
Based on Apurv’s answer and Whirlwind’s comment I thought the following code show work.
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];
}
It doesn’t work and I get errors saying,
Value of type ’SKView?’ has no member ‘sampleTapGestureTapped(recognizerMethod:)’ and
Value of type ’SKView?’ has no member ‘sampleTapGestureTapped(recognizerMethod2:)’
pointing to the declaration of the tap gesture variables
Also, I get the error
Expected expression in the container literal
pointing to the last line in the above method.
I guess I’m missing something right here, I would really appreciate some help and clarifications on this.