Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

touchesBegan, how to get all touches, not just first or last?

In SpriteKit's SKSpriteNodes and other visible nodes, responding to touches in those nodes is often done in some manner like this:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first {...
 // do something with the touch location that's now in touch
// etc

This is all great, but what if I want to make sure I get the touch info of any and all touches inside this (let's imagine a...) SKSpriteNode?

= touches.first limits the conditional to responding only to the first touch inside this SKSpriteNode. How is it done for all touches in this SKSpriteNode, such that each has its location available, or whatever else it's got and carrying around.

I know. Stupid question. I'm that guy. I simply can't see how this is done.

like image 618
Confused Avatar asked Dec 01 '16 06:12

Confused


1 Answers

Usually a view doesn't respond to multiple touches unless the multipleTouchEnabled property is set to true.

mySprite.multipleTouchEnabled = true

You'll notice that the method touchesBegan:event: doesn't return a single UITouch object, but a Set (a.k.a. an unordered collection) of UITouch objects.

Inside this method, instead of just getting the first touch out of the set, you could (for example) iterate over the set in order to check a condition on every touch.

for touch in touches {
  // do something with the touch object
  print(touch.location(mySKNodeObject))
}
like image 139
commscheck Avatar answered Oct 03 '22 10:10

commscheck