gpt4 book ai didi

ios - 如何计算用户拖动图像的速度?

转载 作者:行者123 更新时间:2023-11-29 03:45:39 24 4
gpt4 key购买 nike

我有一个图像,用户可以将其拖动到右侧,当用户释放它时它会弹回来。我想在用户快速拖动并释放它时执行一些代码。现在我有一个非常尴尬的要求,即用户可以拖动图像,然后保持静止任意时间(例如5秒),然后快速拖动并释放它。只要图像在释放时移动超过一定速度,就会执行代码。如果它低于最低速度,它会执行一些不同的代码。所以这意味着我无法计算手势开始和结束之间的时间长度并根据时间长度执行代码。我能做些什么?我想我需要知道手势结束前最后 500 毫秒内图像移动的速度。然而,我在弄清楚如何做到这一点时遇到了困难。任何帮助将不胜感激。

您能否在您的答案中附上解释和可能的示例代码,因为这将是一个很大的帮助。

最佳答案

如果得到了图像拖动时的起始X、Y坐标,以及释放鼠标时的X、Y坐标,则可以利用毕达哥拉斯定理计算出两点之间的距离:http://en.wikipedia.org/wiki/Pythagorean_theorem

此外,如果您在鼠标移动(并且鼠标按钮按下)时启动计时器,并在 mouseup 事件中停止计时器,则可以使用时间和距离计算速度(速度 = 距离/时间)

编辑以下评论:

point delayedMousePos;
point previousMousePos;

bool secondDrag = false;
bool isStopped = false;

var timeFirstStopped;
var positionCount = 0;

array previousMousePositions[3];

// timer which monitors mouse position (set to an interval of say, 10ms)
function timerMonitorMousePos_Elapsed() {
point currentMousePos = getMousePos();

if (isStopped == false) {
if (positionCount >= 2) {
array_shift(previousMousePositions); // remove the first element of the array and move everything down to reindex numerical array to start counting from zero
positionCount = 2; // keep positionCount within array bounds
}

previousMousePositions[positionCount] = currentMousePos; // add the new position to the end of the 'stack'
positionCount++;
}

if (currentMousePos == previousMousePos) { // start check for stationary
isStopped = true;
if (timeFirstStopped == null) {
timeFirstStopped = NOW();
} else {
if (NOW() - timeFirstStopped >= 500) { // we have been stopped for at least 500ms (assumes time is counted in milliseconds)
secondDrag = true;
// previousMousePositions[0] = the mouse position 30ms before the mouse stopped
}
}
} else {
isStopped = false;
timeFirstStopped = null;
}

previousMousePos = currentMousePos;
}

关于ios - 如何计算用户拖动图像的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17792541/

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