Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tracking two fingers with TouchesMoved

Suppose I have two fingers touching iPhone's screen but just one is moving.

TouchesMoved will just show one finger (event).

How do I know which of the two fingers TouchesMoved is referring too?

like image 416
Duck Avatar asked Aug 10 '09 16:08

Duck


3 Answers

Unfortunately if you think about it there's no "definitive" way to associate one finger with one touch point. It isn't, after all, that your fingers have globally unique id's that the iPhone has the ability to sample.

What you need to do is keep a record of the "prior" locations, which is useful for managing pinches and other things anyway - and tag each finger based on the proximity to the prior touches set.

like image 146
groundhog Avatar answered Oct 21 '22 21:10

groundhog


I discovered that it is possible to do what I want. Just check Stanford's CS193P course on iTunesU.

like image 36
Duck Avatar answered Oct 21 '22 23:10

Duck


First, enable multitouch on your UIView:

self.multipleTouchEnabled = true

Then keep a dictionary for the UITouch objects. The same UITouch objects gets passed in touchesBegan, touchesMoved, and touchesEnded:

var touchTypes = Dictionary<UITouch, Int>()

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touchObject in touches {
        touchTypes.updateValue(i, forKey: touch as UITouch) //determine i for your own implementation
    }
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    let type = touchTypes[touch] //depending on this value, do something
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    for touchObject in touches {
        touchTypes.removeValueForKey(touchObject as UITouch)
    }
}
like image 1
Jeffrey Sun Avatar answered Oct 21 '22 22:10

Jeffrey Sun