timestamp and calculating velocity of a swipe

user1437530 picture user1437530 · Jun 17, 2012 · Viewed 8.7k times · Source

Hey I know there are already a few posts about this - yet I still can't find an adequate answer for the problem I'm having.

Just new to cocoa and iOS, I'm in the middle of developing my first iOS game. In this game I would like to be able to calculate the speed of a user's swipe. I'm having no difficulty finding the distance between successive touches in the swipe motion, but am having difficulty determining the time elapsed between the touches

  • in touchesMoved: I do calculations with the current touch as well as keep track of the last previously recorded touch as a UITouch

  • in touchesEnded: I now want to calculate the speed of swipe, but when I do something like:

    double timeDelay = event.timestamp - self.previousTouch.timestamp;

this always returns 0.

However, using gcc I'm able to see that the two timestamps are in fact NOT the same. Additionally, upon inspecting I saw that the NSTimeInterval values for these events were on the magnitude of ~ 10^(-300). This seems odd as an NSTimeInterval is supposed to report seconds since system start up is it not?

I've also tried keeping track of the NSDate of the previous touch and use it with conjunction with [NSDate timeIntervalSinceNow]. This yielded even stranger results, returning a value around 6 every time. Again, since timerIntervalSinceNow returns a NSTimeInterval, this value is very strange.

What am I not understanding about timestamps? events? Any help on this would be greatly appreciated! Thanks for your time

some supporting code:

In sampleController.h:

@property(nonatomic) UITouch* previousTouch
@property(nonatomic) UITouch* currentTouch

In sampleController.m:

@synthesize previousTouch = _previousTouch, currentTouch = _currentTouch;
...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       self.currentTouch = [[event allTouches] anyObject];
       // do stuff with currentTouch
}
...
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
       self.previousTouch = self.currentTouch;
       self.currentTouch = [[event allTouches] anyObject];
       // do stuff with currentTouch
}
...
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
       float distanceMoved = [self.touch locationInView:self.playView].x - 
              [self.touch previousLocationInView:self.playView].x;
       // attempt 1
       double timeElapsed = self.currentTouch.timestamp - self.previousTouch.timestamp;

       // attempt 2
       double timeElapsed = event.timestamp - self.previousTouch.timestamp;

       // do stuff with distanceMoved and timeDelay
}

Answer

spring picture spring · Jun 17, 2012

The UIPanGestureRecognizer has a velocityInView proerty that you may find of use. Docs are here

Also you may find this ray wenderlich tutorial informative.

(and this isn't a smack - but a few google searches turn up loads of material on this sort of thing. Saves all the hair-pulling for the the larger life questions ;-)