如果您獲取圖像拖動時的X,Y座標的起始位置,以及釋放鼠標時的X,Y座標,則可以使用畢達哥拉斯的範數來計算兩點之間的距離:http://en.wikipedia.org/wiki/Pythagorean_theorem
另外,如果你當鼠標移動(和鼠標按鈕向下)啓動定時器,並在鼠標鬆開事件停止它,則可以使用的時間和距離計算速度(速度=距離/時間)
編輯下面的評論:
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;
}
看一看'UIPanGestureRecognizer'' velocityInView'方法。 – bobnoble
你可以發佈一些代碼嗎?正如@bobnoble提到的,如果你使用手勢識別器,那麼獲得速度是微不足道的。 – Can
我正在使用UIPanGestureRecognizer。當我到達我的電腦時,我會在今天發佈一些代碼。爲什麼不需要用UIPanGestureRecognizer獲得速度? –