views:

400

answers:

1

Hi Everyone:

I am wondering how I would go about measuring the time in between touches inside a view in the iPhone SDK. I know that the first event triggered is touchesBegan: and then the last one triggered is touchesEnded:, however I just don't know how I would go about measuring the time that the user has touched the view for. For instance, if they keep their finder in the view for 2 seconds, it will automatically trigger function twoSeconds: or something like that.

Thanks for any help!

+2  A: 

Use [NSDate date] to get the current date and time. Store it in touchesBegan:, and fetch the duration in touchesEnded: as this

NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:beganDate];

You will now have the duration between the events measured in seconds in length.

PeyloW
Hi PeyloW: Thanks for your reply. This sounds like a great way to measure the time. However, in addition to this, would there be some way to automatically preform a task if the user has touched inside the view for a certain amount of time without needing them to release their finder.
PF1
You could call performSelector:withObject:afterDelay: in touchesBegan:, and then do cancelPreviousPerformRequestWithTarget: in touchesEnded:.
PeyloW