5
<title>Touch Events and Abstractions</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Touch Events and Abstractions</h1>
18
<a href="#toc" class="jump">Jump to Table of Contents</a>
22
<div class="yui3-u-3-4">
24
<div class="content"><div class="intro">
25
<p>The <code>event-touch</code> module extends the <a
26
href="index.html#appendixA">whitelist of DOM events</a> to include the
27
native touch and gesture events and adds relevant information to event
30
<p>The <code>event-move</code> module adds a set of abstract events that adapt to
31
the client environment to handle either touch or mouse input.</p>
33
<p>The <code>event-flick</code> module adds a "flick" event on top of the gesture
34
abstraction layer to capture a user flicking an element with mouse or
37
<p>The <code>event-gestures</code> module is a rollup of these three, but will
38
potentially roll in more gesture based events in the future.</p>
41
<h2 id="using-touch-events">Using Touch Events</h2>
43
<p>YUI DOM event support also extends to touch events. To use touch events in your application, you will need to include the <code>event-touch</code> module in your use statement.</p>
45
<p>The set of common low-level touch events, which exist on most touch-enabled OSes are supported:</p>
48
<li><code>touchstart</code></li>
49
<li><code>touchmove</code></li>
50
<li><code>touchend</code></li>
51
<li><code>touchcancel</code></li>
54
<p>Additionally, the iOS specific touch events, <code>gesturestart</code>,
55
<code>gesturechange</code> and <code>gestureend</code>, are also supported.
56
YUI doesn't replicate support for these events on other touch OSes currently,
57
due to their lack of DOM level multi-touch support. At the point at which they
58
do expose multi-touch information in the lower level touch events, we can build
59
in cross-platform support for multi-touch gestures.</p>
61
<pre class="code prettyprint">node.on('touchstart`, function () {
62
this.addClass('detached');
66
<h4 id="the-touch-event-facade">The Touch Event Facade</h4>
68
<p>The event facade passed to touch events has the common set of touch specific
69
array properties available:</p>
72
<li><code>touches</code></li>
73
<li><code>changedTouches</code></li>
74
<li><code>touchTargets</code></li>
77
<p>These event objects in these arrays are also normalized, just the same as
78
the event object pass to any other DOM listener.</p>
80
<p>The event object for the iOS gesture events have <code>scale</code> and
81
<code>rotation</code> properties, the same as the native event object.</p>
83
<h2 id="move">Cross-Device Gesture Support</h2>
85
<p>The <code>event-move</code> module provides the following events that
86
<em>roughly</em> relate to the associated touch or mouse event, depending on the client
92
<th>Abstract event</th>
99
<td><strong><code>gesturemovestart</code></strong></td>
100
<td><code>mousedown</code></td>
101
<td><code>touchstart</code></td>
104
<td><strong><code>gesturemove</code></strong></td>
105
<td><code>mousemove</code></td>
106
<td><code>touchmove</code></td>
109
<td><strong><code>gesturemoveend</code></strong></td>
110
<td><code>mouseup</code></td>
111
<td><code>touchend</code></td>
116
<p>I say "roughly" because the gesture events aim to encapsulate common
117
interactions rather than just serving as a relay to other events. Where this
118
comes out is in the additional configuration that can be included in the
119
subscription as a third argument.</p>
121
<pre class="code prettyprint">// Notify me when the user puts a finger down, or mouses down on
122
// the car node
123
car.on("gesturemovestart", alignForMove, {
125
// fire the event only after 300ms of continuous contact...
128
// ...or if the finger/mouse moves more than 3px
132
// Move the car node when the user moves the finger or mouse.
133
// Note the `this` override parameter is shifted to account for
134
// the configuration param
135
car.on("gesturemove", car.move, null, car);
138
// Notify me when the user lifts their finger, or lets go of
139
// the mouse button (only if a gesturemovestart was received
140
// on the node).
141
car.on("gesturemoveend", screechingHalt);</pre>
144
<p>The complete set of configuration parameters for the gesture events is <a href="http://yuilibrary.com/yui/docs/api/module_event-move.html#event_gesturemove">in the API docs</a>.</p>
146
<h4 id="related-events">Related Events</h4>
148
<p>The three gesture events are related to each other. They are notifications
149
for the start, progress, and end of the same gesture. <code>gesturemove</code> and
150
<code>gesturemoveend</code> subscriptions won't execute unless a <code>gesturemovestart</code>
153
<p>If you need them to fire separately, such as when attaching and detaching
154
subscribers dynamically, the <code>gesturemove</code> and <code>gesturemoveend</code> events can be
155
subscribed to individually by configuring <code>standAlone: true</code></p>
157
<pre class="code prettyprint">// Doesn't require an associated `gesturemovestart` subscription
158
Y.one("doc").on("gesturemove", function(e) {...}, {
163
<p>Under the hood, the DOM listeners which monitor mousemove/touchmove and
164
mouseup/touchend are bound to the <code>document</code> by default. The node only provides
165
the shared context to relate the three events.</p>
168
<h2 id="flick">Flick Gesture Event</h2>
170
<p>The flick gesture event is fired whenever the user initiates a flick gesture (with a finger or the mouse) on the node where the listener is attached.</p>
172
<pre class="code prettyprint">myNode.on("flick", function(e) {
174
// Some of the flick specific data on the event facade
177
velocity = flick.velocity,
178
distance = flick.distance,
180
startX = flick.start.pageX,
181
startY = flick.start.pageY,
183
// The event object itself is the event object for
184
// the event which concludes the flick (the mouseup or touchend)
188
endTarget = e.target;
193
<p>Like with the supporting gesture events, when subscribing to
194
<code>flick</code>, you can also pass additional configuration to control
195
when and how the flick subscriber is notified.</p>
197
<pre class="code prettyprint">// Custom config, with no context or extra args
198
myNode.on("flick", flickHandler, {
200
// only notify me if the flick covered
201
// more than 20px and was faster than 0.8 px/ms
205
// prevent the default behavior for the
206
// underlying mouse/touch events
210
// Another option to avoid confusion when specifying the `this`
211
// override or bound arguments for events with custom signatures
212
// is to use Y.bind
213
myNode.on("flick", Y.bind(o.flickHandler, o, arg1), {
219
// Alternately, make sure to account for the additional subscription
220
// parameter by passing null if there is no configuration.
221
myNode.on("flick", o.flickHandler, null, o, arg1);</pre>
224
<p>The complete set of configuration parameters for the <code>flick</code> event is <a href="http://yuilibrary.com/yui/docs/api/module_event-flick.html#event_flick">in the API docs</a>.</p>
226
<h2 id="caveats">Caveats</h2>
229
<li>The <code>flick</code> event doesn't (yet) support delegation.</li>
231
The <code>delegate()</code> signature for the gesture events is
232
<code>node.delegate('gesturemove', callback, <em>filter</em>,
233
<em>gestureConfig</em>)</code>, reversing the order of the delegation
234
filter and extra params from the <a href="hover.html"><code>hover</code></a> and
235
<a href="key.html"><code>key</code></a> events. This may be changed in a future
244
<div class="yui3-u-1-4">
245
<div class="sidebar">
247
<div id="toc" class="sidebox">
249
<h2 class="no-toc">Table of Contents</h2>
255
<a href="#using-touch-events">Using Touch Events</a>
258
<a href="#the-touch-event-facade">The Touch Event Facade</a>
263
<a href="#move">Cross-Device Gesture Support</a>
266
<a href="#related-events">Related Events</a>
271
<a href="#flick">Flick Gesture Event</a>
274
<a href="#caveats">Caveats</a>
282
<div class="sidebox">
284
<h2 class="no-toc">Examples</h2>
288
<ul class="examples">
291
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
292
<a href="basic-example.html">Simple DOM Events</a>
297
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed.">
298
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a>
303
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events">
304
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a>
324
<div class="sidebox">
326
<h2 class="no-toc">Examples That Use This Component</h2>
330
<ul class="examples">
339
<li data-description="Shows how to extend the base widget class, to create your own Widgets.">
340
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a>
345
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States.">
346
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
351
<li data-description="Use IO to request data over HTTP.">
352
<a href="../io/get.html">HTTP GET to request data</a>
357
<li data-description="Example Photo Browser application.">
358
<a href="../dd/photo-browser.html">Photo Browser</a>
363
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
364
<a href="../dd/portal-drag.html">Portal Style Example</a>
377
<script src="../assets/vendor/prettify/prettify-min.js"></script>
378
<script>prettyPrint();</script>