gpt4 book ai didi

iphone - UITouch 移动速度检测

转载 作者:太空狗 更新时间:2023-10-30 03:23:59 25 4
gpt4 key购买 nike

我正在尝试检测触摸移动的速度,但并不总能得到预期的结果。


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

}

最佳答案

您可以尝试(将 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 的值。

关于iphone - UITouch 移动速度检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/668247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com