Not sure how to format code on this forum …
I have the following code which for the most part works. However
touchesEnded only fires when ALL fingers are removed. This is inside a
UIInputViewController and the view has multiTouchEnabled = true. The touchesBegin does fire for each finger down.
How do I make it work so I get touchesEnded as each finger is removed?
var activeTouches = [UITouch: Key]()
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
for touch in touches {
let pressPoint = touch.locationInView(self.view)
if let key = self.view.hitTestKey(pressPoint, withEvent: nil) as? Key {
activeTouches[touch] = key
key.sendActionsForControlEvents(.TouchDown)
keyboardClick()
}
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesMoved(touches, withEvent: event)
for touch in touches {
let pressPoint = touch.locationInView(self.view)
let key = self.view.hitTestKey(pressPoint, withEvent: nil) as? Key
if activeTouches[touch] != key {
if let previousKey = activeTouches[touch] {
previousKey.sendActionsForControlEvents(.TouchCancel)
}
if key != nil {
activeTouches[touch] = key
key!.sendActionsForControlEvents(.TouchDown)
} else {
activeTouches.removeValueForKey(touch)
}
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
for touch in touches {
if let key = activeTouches[touch] {
key.sendActionsForControlEvents(.TouchUpInside)
activeTouches.removeValueForKey(touch)
}
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
if touches == nil {
for key in activeTouches.values {
key.sendActionsForControlEvents(.TouchCancel)
}
activeTouches.removeAll()
} else {
for touch in touches! {
if let key = activeTouches[touch] {
key.sendActionsForControlEvents(.TouchCancel)
activeTouches.removeValueForKey(touch)
}
}
}
}