当前位置:  开发笔记 > 编程语言 > 正文

UITouch移动速度检测

如何解决《UITouch移动速度检测》经验,为你挑选了1个好方法。

我试图检测触摸运动的速度,我并不总是得到我期望的结果.(补充说:速度太快了)如果我正在做一些时髦或建议更好的方法,有人能发现吗?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    self.previousTimestamp = event.timestamp;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    CGPoint prevLocation = [touch previousLocationInView:self.view];
    CGFloat distanceFromPrevious = distanceBetweenPoints(location,prevLocation);
    NSTimeInterval timeSincePrevious = event.timestamp - self.previousTimestamp;
    CGFloat speed = distanceFromPrevious/timeSincePrevious;
    self.previousTimestamp = event.timestamp;
    NSLog(@"dist %f | time %f | speed %f",distanceFromPrevious, timeSincePrevious, speed);

}

Jim.. 11

你可以尝试(在touchesBegan中将distanceSinceStart和timeSinceStart归零):

distanceSinceStart = distanceSinceStart + distanceFromPrevious;
timeSinceStart = timeSincestart + timeSincePrevious;
speed = distanceSinceStart/timeSinceStart;

这将为您提供自开始触摸以来的平均速度(总距离/总时间).

或者你可以做一个移动平均速度,也许是一个指数移动平均线:

const float lambda = 0.8f; // the closer to 1 the higher weight to the next touch

newSpeed = (1.0 - lambda) * oldSpeed + lambda* (distanceFromPrevious/timeSincePrevious);
oldSpeed = newSpeed;

如果要为最近的值赋予更多权重,可以将lambda调整为接近1的值.



1> Jim..:

你可以尝试(在touchesBegan中将distanceSinceStart和timeSinceStart归零):

distanceSinceStart = distanceSinceStart + distanceFromPrevious;
timeSinceStart = timeSincestart + timeSincePrevious;
speed = distanceSinceStart/timeSinceStart;

这将为您提供自开始触摸以来的平均速度(总距离/总时间).

或者你可以做一个移动平均速度,也许是一个指数移动平均线:

const float lambda = 0.8f; // the closer to 1 the higher weight to the next touch

newSpeed = (1.0 - lambda) * oldSpeed + lambda* (distanceFromPrevious/timeSincePrevious);
oldSpeed = newSpeed;

如果要为最近的值赋予更多权重,可以将lambda调整为接近1的值.


不是......这是你自己指定的常数.它越接近1,您对最新值的重量就越大.与n值的算术平均值进行比较.每个新值的权重为1/n.对于指数,设置lambda = 2 /(n + 1),其中n是等效算术值.所以新值加权2 /(n + 1)而不是1/n,然后现有的移动平均值按(1-lambda)=(n-1)/(n + 1)缩小,两者是添加.更清晰?
推荐阅读
U友50081205_653
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有