时间戳和计算滑动的速度 [英] timestamp and calculating velocity of a swipe

查看:621
本文介绍了时间戳和计算滑动的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我知道已经有一些关于此的帖子 - 但我仍然无法找到适合我所遇问题的答案。

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.

刚刚开始使用cocoa和iOS,我正在开发我的第一款iOS游戏。在这个游戏中,我希望能够计算用户滑动的速度。我在滑动动作中找到连续触摸之间的距离没有任何困难,但是很难确定触摸之间经过的时间

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:我使用当前触摸进行计算,并将之前记录的最后一次触摸记录为UITouch

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

touchesEnded:我现在想要计算滑动的速度,但是当我做以下的事情时:

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

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

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

这总是返回0 。

然而,使用gcc我能够看到两个时间戳实际上并不相同。另外,在检查时,我看到这些事件的NSTimeInterval值大约为10 ^( - 300)。这看起来很奇怪,因为NSTimeInterval应该报告秒,因为系统启动不是吗?

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?

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

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

一些支持代码:

在sampleController.h中:

In sampleController.h:

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

在sampleController.m中:

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
}


推荐答案

UIPanGestureRecognizer 有一个你可能会发现使用的 velocityInView proerty。文档是此处

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

此外,您可能会发现此光线wenderlich 教程提供信息。

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

这篇关于时间戳和计算滑动的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆