Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Add rotate gesture #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
153 changes: 152 additions & 1 deletion dist/ember-touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ Ember.Application.initializer({
gestureManager.registerGesture('swipe', Em.SwipeGestureRecognizer);
gestureManager.registerGesture('tap', Em.TapGestureRecognizer);
gestureManager.registerGesture('touchHold', Em.TouchHoldGestureRecognizer);

gestureManager.registerGesture('rotate', Em.RotateGestureRecognizer);
}

});
Expand Down Expand Up @@ -2358,6 +2358,157 @@ Em.SwipeGestureRecognizer = Em.Gesture.extend({



(function() {
var get = Em.get, set = Em.set;

/**
@module ember
@submodule ember-touch
*/
/**
Recognizes a rotate gesture. Rotations are continuous gestures that will get fired on a view. The rotation gesture
tracks the changes in the angle between two fingers.

var myview = Em.View.create({

rotateStart: function(recognizer, evt) {

},
// usually, you will only use this method
rotateChange: function(recognizer, evt) {
var rotationDegrees = recognizer.get('rotationDegrees'), // total rotation in degrees since gesture start
rotation = recognizer.get('rotation'), // total rotation in radians since gesture start
deltaDegrees = recognizer.get('deltaDegrees'), // rotation in degrees since previous call to rotateChange
delta = recognizer.get('delta'); // rotation in radians since previous call to rotateChange

this.$().css('rotation', rotationDegrees + 'deg');
},
rotateEnd: function(recognizer, evt) {

},
rotateCancel: function(recognizer, evt) {

}
});

@class RotateGestureRecognizer
@namespace Ember
@extends Em.Gesture
*/

Em.RotateGestureRecognizer = Em.Gesture.extend({
/**
total rotation in radians since gesture start

@type Number
*/
rotation: 0,

/**
total rotation in degrees since gesture start

@type Number
*/
rotationDegrees: 0,

/**
rotation in radians since previous call to rotateChange

@type Number
*/
delta: 0,

/**
rotation in radians since previous call to rotateChange

@type Number
*/
deltaDegrees: 0,

numberOfRequiredTouches: 2,

/**
@private
*/
DEGREES_PER_RADIAN: 180 / Math.PI,

/**
@private
*/
startAngle: 0,
/**
@private
*/
previousAngle: 0,
/**
@private
*/
previousDelta: 0,

/**
@private
*/
angle: 0,


didBecomePossible: function () {
var angle = this.angleBetweenTouches();

set(this, 'startAngle', angle);
set(this, 'angle', angle);
set(this, 'previousAngle', angle);
set(this, 'previousDelta', 0);
set(this, 'delta', 0);

this.updateAngles(this.angleBetweenTouches());
},
shouldBegin: function () {
return true;
},
didChange: function () {
this.updateAngles(this.angleBetweenTouches());
},
eventWasRejected: function () {
var previousDelta = get(this, 'previousDelta'),
previousAngle = get(this, 'previousAngle');

this.updateAngles(previousAngle);

set(this, 'delta', previousDelta);
set(this, 'deltaDegrees', previousDelta * this.DEGREES_PER_RADIAN);
},
angleBetweenTouches: function () {
var touches = get(this.touches, 'touches'),
dx = touches[1].pageX - touches[0].pageX,
dy = touches[1].pageY - touches[0].pageY;

return Math.atan2(dy, dx);
},
updateAngles: function (angle) {
var startAngle = get(this, 'startAngle'),
previousAngle = get(this, 'angle'),
previousDelta = get(this, 'delta'),
rotation = startAngle - angle,
delta = previousAngle - angle;

set(this, 'previousAngle', previousAngle);
set(this, 'previousDelta', previousDelta);
set(this, 'angle', angle);
set(this, 'rotation', rotation);
set(this, 'rotationDegrees', rotation * this.DEGREES_PER_RADIAN);
set(this, 'delta', delta);
set(this, 'deltaDegrees', delta * this.DEGREES_PER_RADIAN);
},
toString: function () {
return Em.RotateGestureRecognizer + '<' + Em.guidFor(this) + '>';
}
});


})();



(function() {

})();
Expand Down
1 change: 1 addition & 0 deletions packages/ember-touch/lib/gesture_recognizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ require('ember-touch/gesture_recognizers/tap');
require('ember-touch/gesture_recognizers/press');
require('ember-touch/gesture_recognizers/touch_hold');
require('ember-touch/gesture_recognizers/swipe');
require('ember-touch/gesture_recognizers/rotate');
147 changes: 147 additions & 0 deletions packages/ember-touch/lib/gesture_recognizers/rotate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
require('ember-touch/system/gesture');

var get = Em.get, set = Em.set;

/**
@module ember
@submodule ember-touch
*/
/**
Recognizes a rotate gesture. Rotations are continuous gestures that will get fired on a view. The rotation gesture
tracks the changes in the angle between two fingers.

var myview = Em.View.create({

rotateStart: function(recognizer, evt) {

},
// usually, you will only use this method
rotateChange: function(recognizer, evt) {
var rotationDegrees = recognizer.get('rotationDegrees'), // total rotation in degrees since gesture start
rotation = recognizer.get('rotation'), // total rotation in radians since gesture start
deltaDegrees = recognizer.get('deltaDegrees'), // rotation in degrees since previous call to rotateChange
delta = recognizer.get('delta'); // rotation in radians since previous call to rotateChange

this.$().css('rotation', rotationDegrees + 'deg');
},
rotateEnd: function(recognizer, evt) {

},
rotateCancel: function(recognizer, evt) {

}
});

@class RotateGestureRecognizer
@namespace Ember
@extends Em.Gesture
*/

Em.RotateGestureRecognizer = Em.Gesture.extend({
/**
total rotation in radians since gesture start

@type Number
*/
rotation: 0,

/**
total rotation in degrees since gesture start

@type Number
*/
rotationDegrees: 0,

/**
rotation in radians since previous call to rotateChange

@type Number
*/
delta: 0,

/**
rotation in radians since previous call to rotateChange

@type Number
*/
deltaDegrees: 0,

numberOfRequiredTouches: 2,

/**
@private
*/
DEGREES_PER_RADIAN: 180 / Math.PI,

/**
@private
*/
startAngle: 0,
/**
@private
*/
previousAngle: 0,
/**
@private
*/
previousDelta: 0,

/**
@private
*/
angle: 0,


didBecomePossible: function () {
var angle = this.angleBetweenTouches();

set(this, 'startAngle', angle);
set(this, 'angle', angle);
set(this, 'previousAngle', angle);
set(this, 'previousDelta', 0);
set(this, 'delta', 0);

this.updateAngles(this.angleBetweenTouches());
},
shouldBegin: function () {
return true;
},
didChange: function () {
this.updateAngles(this.angleBetweenTouches());
},
eventWasRejected: function () {
var previousDelta = get(this, 'previousDelta'),
previousAngle = get(this, 'previousAngle');

this.updateAngles(previousAngle);

set(this, 'delta', previousDelta);
set(this, 'deltaDegrees', previousDelta * this.DEGREES_PER_RADIAN);
},
angleBetweenTouches: function () {
var touches = get(this.touches, 'touches'),
dx = touches[1].pageX - touches[0].pageX,
dy = touches[1].pageY - touches[0].pageY;

return Math.atan2(dy, dx);
},
updateAngles: function (angle) {
var startAngle = get(this, 'startAngle'),
previousAngle = get(this, 'angle'),
previousDelta = get(this, 'delta'),
rotation = startAngle - angle,
delta = previousAngle - angle;

set(this, 'previousAngle', previousAngle);
set(this, 'previousDelta', previousDelta);
set(this, 'angle', angle);
set(this, 'rotation', rotation);
set(this, 'rotationDegrees', rotation * this.DEGREES_PER_RADIAN);
set(this, 'delta', delta);
set(this, 'deltaDegrees', delta * this.DEGREES_PER_RADIAN);
},
toString: function () {
return Em.RotateGestureRecognizer + '<' + Em.guidFor(this) + '>';
}
});

2 changes: 1 addition & 1 deletion packages/ember-touch/lib/system/application_ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Ember.Application.initializer({
gestureManager.registerGesture('swipe', Em.SwipeGestureRecognizer);
gestureManager.registerGesture('tap', Em.TapGestureRecognizer);
gestureManager.registerGesture('touchHold', Em.TouchHoldGestureRecognizer);

gestureManager.registerGesture('rotate', Em.RotateGestureRecognizer);
}

});
Loading