5 iphone timestamp uitouch swipe ios
嘿,我知道已经有一些关于此事的帖子 - 但我仍然无法找到适合我所遇问题的答案.
刚刚开始使用cocoa和iOS,我正在开发我的第一款iOS游戏.在这个游戏中,我希望能够计算用户滑动的速度.我在滑动动作中找到连续触摸之间的距离没有任何困难,但我很难确定触摸之间经过的时间
在touchesMoved:我用当前触摸进行计算以及跟踪最后一次记录的触摸作为UITouch
在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
Run Code Online (Sandbox Code Playgroud)
在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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8394 次 |
| 最近记录: |