Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swipe recognizer using overall gesture direction and velocity instead of last interval #669

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,17 @@ function computeInputData(manager, input) {

computeDeltaXY(session, input);
input.offsetDirection = getDirection(input.deltaX, input.deltaY);


var overallVelocity = getVelocity(input.deltaTime, input.deltaX, input.deltaY);
input.overallVelocityX = overallVelocity.x;
input.overallVelocityY = overallVelocity.y;
input.overallVelocity = (abs(overallVelocity.x) > abs(overallVelocity.y)) ? overallVelocity.x : overallVelocity.y;

input.scale = firstMultiple ? getScale(firstMultiple.pointers, pointers) : 1;
input.rotation = firstMultiple ? getRotation(firstMultiple.pointers, pointers) : 0;

input.maxPointers = !session.prevInput ? input.pointers.length : ((input.pointers.length > session.prevInput.maxPointers) ? input.pointers.length : session.prevInput.maxPointers);

computeIntervalInputData(session, input);

// find the correct target
Expand Down Expand Up @@ -220,8 +227,8 @@ function computeIntervalInputData(session, input) {
velocity, velocityX, velocityY, direction;

if (input.eventType != INPUT_CANCEL && (deltaTime > COMPUTE_INTERVAL || last.velocity === undefined)) {
var deltaX = last.deltaX - input.deltaX;
var deltaY = last.deltaY - input.deltaY;
var deltaX = input.deltaX - last.deltaX;
var deltaY = input.deltaY - last.deltaY;

var v = getVelocity(deltaTime, deltaX, deltaY);
velocityX = v.x;
Expand Down Expand Up @@ -326,9 +333,9 @@ function getDirection(x, y) {
}

if (abs(x) >= abs(y)) {
return x > 0 ? DIRECTION_LEFT : DIRECTION_RIGHT;
return x < 0 ? DIRECTION_LEFT : DIRECTION_RIGHT;
}
return y > 0 ? DIRECTION_UP : DIRECTION_DOWN;
return y < 0 ? DIRECTION_UP : DIRECTION_DOWN;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/recognizers/swipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ inherit(SwipeRecognizer, AttrRecognizer, {
var velocity;

if (direction & (DIRECTION_HORIZONTAL | DIRECTION_VERTICAL)) {
velocity = input.velocity;
velocity = input.overallVelocity;
} else if (direction & DIRECTION_HORIZONTAL) {
velocity = input.velocityX;
velocity = input.overallVelocityX;
} else if (direction & DIRECTION_VERTICAL) {
velocity = input.velocityY;
velocity = input.overallVelocityY;
}

return this._super.attrTest.call(this, input) &&
direction & input.direction &&
direction & input.offsetDirection &&
input.distance > this.options.threshold &&
input.maxPointers == this.options.pointers &&
abs(velocity) > this.options.velocity && input.eventType & INPUT_END;
},

emit: function(input) {
var direction = directionStr(input.direction);
var direction = directionStr(input.offsetDirection);
if (direction) {
this.manager.emit(this.options.event + direction, input);
}
Expand Down