时间戳和计算滑动的速度

嘿,我知道已经有几个关于这个的文章 – 但我仍然无法find一个适当的答案,我遇到的问题。

刚开始cocoa和iOS,我正在开发我的第一个iOS游戏。 在这个游戏中,我希望能够计算用户的滑动速度。 我毫不费力地find了滑动运动中连续触摸之间的距离,但是难以确定触摸之间所经过的时间

  • touchesMoved:我做了当前的触摸计算,以及作为一个UITouch跟踪最近以前logging的触摸

  • touchesEnded:我现在想要计算刷卡的速度,但是当我做这样的事情:

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

这总是返回0。

但是,使用gcc我能够看到两个时间戳实际上是不一样的。 此外,在检查时,我看到这些事件的NSTimeInterval值在〜10 ^( – 300)的范围内。 这似乎很奇怪,因为NSTimeInterval应该报告几秒钟,因为系统启动不是?

我也试着跟踪上一次触摸的NSDate,并与[NSDate timeIntervalSinceNow]结合使用。 这产生了更奇怪的结果,每次返回大约6的值。 同样,由于timerIntervalSinceNow返回一个NSTimeInterval ,这个值非常奇怪。

什么是我不了解时间戳? 事件? 任何帮助,将不胜感激! 谢谢你的时间

一些支持代码:

在sampleController.h中:

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

在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 } 

UIPanGestureRecognizer有一个velocityInView proerty,你可以find使用。 文档在这里

你也可以find这个光芒wenderlich 教程信息。

(这并不是什么好玩的事情 – 但是有一些谷歌search会在这类事情上增加大量的资料。