views:

760

answers:

3

Is there any function in cocos2d or objective c to find the time between touch began and touch ended??

A: 

In your touchesBegan:withEvent: method, create an NSDate object and store it in an instance variable. (For non-debugging purposes, create one date per touch and use CFMutableDictionary to associate the date with the touch.) Then, in touchesEnded:withEvent:, retrieve the stored date and create a new date, and send the latter a timeIntervalSinceDate: message to subtract the older date from it, giving you the result in seconds.

Peter Hosey
It's my understanding cocos2d takes care of implementing these methods and abstracts them away.
jbrennan
+1  A: 

Each UITouch has it's own time/date stamp. Compare the ones you're interested in. Take a looke at the UIResponder class ref.

Aside- SO needs to update their code or release an iPhone app. Typing into their javascriptoided out text fields with an iPhone is almost comical.

Meltemi
+7  A: 
// add this ivar to your view controller
NSTimeInterval lastTouch;

 // assign the time interval in touchesBegan:
 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
 {
     lastTouch = [event timestamp];
 }  


 // calculate and print interval in touchesEnded:
 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
{
 NSTimeInterval touchBeginEndInterval = [event timestamp] - lastTouch;

 NSLog(@"touchBeginEndInterval %f", touchBeginEndInterval);
}
willc2