Ton Marton Ton Marton - 7 months ago 12
Swift Question

Exceeding the maximum number of touches makes the program not to call the touchesEnded method?

Ive just started learning programming in general and ran into a problem:
I have read some articles about that the iPhones are only capable of tracking 5 touches or so... But I realised that when I touch the screen with e.g. 7 fingers at once, my program-let stops working.

So, anyone knows, which parts of the code malfunctions when I am touching the screen way too many times at once?

(I am a newbie.)

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
touchLocation = touch.locationInNode(self)
nrTouches += touches.count
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
nrTouches -= touches.count
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
touchLocation = touch.locationInNode(self)
}
override func update(currentTime: CFTimeInterval) {
if nrTouches > 0 {
touchingLabel.text = "touching"
} else {
touchingLabel.text = "not touching"
}
}


So, in the case I am touching with 7 fingers at once,
"touching"
will be displayed all the time.
tankyuu

Answer

You're missing touchesCancelled, which gets called when the 6th finger is put down:

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) { 
    if let touches = touches {
        nrTouches -= touches.count // same logic as touchesEnded
    }
}

An interesting note: The iPad can handle 11 touch events. More on that here.