Time stamp and swipe speed calculation

Hey, I know that there are already a few posts about this, but I still can’t find an adequate answer to the problem that I have.

Just new to cocoa and iOS, I'm in the middle of developing my first iOS game. In this game, I would like to calculate the user's scroll speed. I do not find it difficult to find the distance between successive touches in the movement of the swipe, but it is difficult for me to determine the time elapsed between touches

  • in touchesMoved:I perform calculations with the current touch, and also track the last previously recorded touch as UITouch

  • in touchesEnded:Now I want to calculate the scroll speed, but when I do something like:

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

it always returns 0.

However, using gcc, I can see that the two timestamps do not actually match. In addition, after checking, I saw that the NSTimeInterval values ​​for these events were ~ 10 ^ (- 300). This seems strange, since NSTimeInterval should report seconds since system startup, right?

I also tried to track the NSDate of the previous touch and use it in combination with [NSDate timeIntervalSinceNow]. This gave even stranger results, each time returning a value of about 6. Again, since timerIntervalSinceNow returns a NSTimeInterval, this value is very strange.

What I do not understand about timestamps? Activity? Any help on this subject would be greatly appreciated! thank you 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
}
+5
1

UIPanGestureRecognizer velocityInView proerty, .

wenderlich tutorial .

( ), Google . ; -)

+4

All Articles