5

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
}
Cezar
  • 55,636
  • 19
  • 86
  • 87

1 Answers1

4

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 ;-)

spring
  • 18,009
  • 15
  • 80
  • 160
  • I'm aware of the UIPanGestureRecognizer but am having trouble with that as well. Perhaps some more background on what I'm working on; the game is of a brick-beaker variety, and as such I'm constantly using touchesBegan: and touchesMoved: to update the state of the paddle ect. I've tried using a UIPanGestureRecognizer to solely update the speed of the swiping using [sender velocityInView:] inside the target's action selector method. However when doing this, whenever a UIPanGesture was recognized, it interrupts the touchesMoved: functionality some how. Advice? –  Jun 17 '12 at 14:26
  • sorry, found out about the cancelsTouchesInView property. Works perfectly now. –  Jun 17 '12 at 14:30
  • 2
    As an aside, I find the default velocity metric to be annoying because if you are panning, stop, and then release your finger, the velocity is the value prior to stopping, whereas I'd like it to be zero if you really stopped. Am I the only one who finds this so annoying? – Rob Jun 21 '12 at 22:41