Skip to content

Custom Gestures

jtangelder edited this page Feb 27, 2013 · 4 revisions

####Gesture object#### The object structure of a gesture:

{ 
  name: 'mygesture',
  index: 1337,
  defaults: {
     mygesture_option: true
  }
  handler: function(type, ev, inst) {
     // trigger gesture event
     inst.trigger(this.name, ev);
  }
}
{String} name

this should be the name of the gesture, lowercase it is also being used to disable/enable the gesture per instance config.

{Number} [index=1000]

the index of the gesture, where it is going to be in the stack of gestures detection like when you build an gesture that depends on the drag gesture, it is a good idea to place it after the index of the drag gesture.

{Object} [defaults={}]

the default settings of the gesture. these are added to the instance settings, and can be overruled per instance. you can also add the name of the gesture, but this is also added by default (and set to true).

{Function} handler

this handles the gesture detection of your custom gesture and receives the following arguments:

{Object} eventData

As described above, its the same data as the gesture events

{Hammer.Instance} inst

the instance we are doing the detection for. you can get the options from the inst.options object and trigger the gesture event by calling inst.trigger

####Handle gestures#### inside the handler you can get/set Hammer.detection.current. This is the current detection session. It has the following properties

{String} name

contains the name of the gesture we have detected. it has not a real function, only to check in other gestures if something is detected. like in the drag gesture we set it to 'drag' and in the swipe gesture we can check if the current gesture is 'drag' by accessing Hammer.detection.current.name

@readonly {Hammer.Instance} inst

the instance we do the detection for

@readonly {Object} startEvent

contains the properties of the first gesture detection in this session. Used for calculations about timing, distance, etc.

@readonly {Object} lastEvent

contains all the properties of the last gesture detect in this session.

after the gesture detection session has been completed (user has released the screen) the Hammer.detection.current object is copied into Hammer.detection.previous, this is usefull for gestures like doubletap, where you need to know if the previous gesture was a tap

options that have been set by the instance can be received by calling inst.options

You can trigger a gesture event by calling inst.trigger("mygesture", event). The first param is the name of your gesture, the second the event argument

####Register gestures#### When an gesture is added to the Hammer.gestures object, it is auto registered at the setup of the first Hammer instance. You can also call Hammer.detection.register manually and pass your gesture object as a param

Clone this wiki locally