|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Touch Events and Abstractions</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
<a href="#toc" class="jump">Jump to Table of Contents</a> |
|
|
24 |
|
|
|
25 |
|
|
|
26 |
<h1>Touch Events and Abstractions</h1> |
|
|
27 |
<div class="yui3-g"> |
|
|
28 |
<div class="yui3-u-3-4"> |
|
|
29 |
<div id="main"> |
|
|
30 |
<div class="content"><div class="intro"> |
|
|
31 |
<p>The <code>event-touch</code> module extends the <a |
|
|
32 |
href="index.html#event-whitelist">whitelist of DOM events</a> to include the |
|
|
33 |
native touch and gesture events and adds relevant information to event |
|
|
34 |
facades.</p> |
|
|
35 |
|
|
|
36 |
<p>The <code>event-move</code> module adds a set of abstract events that adapt to |
|
|
37 |
the client environment to handle either touch or mouse input.</p> |
|
|
38 |
|
|
|
39 |
<p>The <code>event-flick</code> module adds a "flick" event on top of the gesture |
|
|
40 |
abstraction layer to capture a user flicking an element with mouse or |
|
|
41 |
finger.</p> |
|
|
42 |
|
|
|
43 |
<p>The <code>event-gestures</code> module is a rollup of these three, but will |
|
|
44 |
potentially roll in more gesture based events in the future.</p> |
|
|
45 |
</div> |
|
|
46 |
|
|
|
47 |
<h2 id="using-touch-events">Using Touch Events</h2> |
|
|
48 |
|
|
|
49 |
<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> |
|
|
50 |
|
|
|
51 |
<p>The set of common low-level touch events, which exist on most touch-enabled OSes are supported:</p> |
|
|
52 |
|
|
|
53 |
<ul> |
|
|
54 |
<li><code>touchstart</code></li> |
|
|
55 |
<li><code>touchmove</code></li> |
|
|
56 |
<li><code>touchend</code></li> |
|
|
57 |
<li><code>touchcancel</code></li> |
|
|
58 |
</ul> |
|
|
59 |
|
|
|
60 |
<p>Additionally, the iOS specific touch events, <code>gesturestart</code>, |
|
|
61 |
<code>gesturechange</code> and <code>gestureend</code>, are also supported. |
|
|
62 |
YUI doesn't replicate support for these events on other touch OSes currently, |
|
|
63 |
due to their lack of DOM level multi-touch support. At the point at which they |
|
|
64 |
do expose multi-touch information in the lower level touch events, we can build |
|
|
65 |
in cross-platform support for multi-touch gestures.</p> |
|
|
66 |
|
|
|
67 |
<pre class="code prettyprint">node.on("touchstart", function () { |
|
|
68 |
this.addClass("detached"); |
|
|
69 |
});</pre> |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
<h4 id="the-touch-event-facade">The Touch Event Facade</h4> |
|
|
73 |
|
|
|
74 |
<p>The event facade passed to touch events has the common set of touch specific |
|
|
75 |
array properties available:</p> |
|
|
76 |
|
|
|
77 |
<ul> |
|
|
78 |
<li><code>touches</code></li> |
|
|
79 |
<li><code>changedTouches</code></li> |
|
|
80 |
<li><code>touchTargets</code></li> |
|
|
81 |
</ul> |
|
|
82 |
|
|
|
83 |
<p>These event objects in these arrays are also normalized, just the same as |
|
|
84 |
the event object pass to any other DOM listener.</p> |
|
|
85 |
|
|
|
86 |
<p>The event object for the iOS gesture events have <code>scale</code> and |
|
|
87 |
<code>rotation</code> properties, the same as the native event object.</p> |
|
|
88 |
|
|
|
89 |
<h2 id="move">Cross-Device Gesture Support</h2> |
|
|
90 |
|
|
|
91 |
<p>The <code>event-move</code> module provides the following events that |
|
|
92 |
<em>roughly</em> relate to the associated touch or mouse event, depending on the client |
|
|
93 |
device:</p> |
|
|
94 |
|
|
|
95 |
<table> |
|
|
96 |
<thead> |
|
|
97 |
<tr> |
|
|
98 |
<th>Abstract event</th> |
|
|
99 |
<th>Mouse event</th> |
|
|
100 |
<th>Touch event</th> |
|
|
101 |
</th> |
|
|
102 |
</thead> |
|
|
103 |
<tbody> |
|
|
104 |
<tr> |
|
|
105 |
<td><strong><code>gesturemovestart</code></strong></td> |
|
|
106 |
<td><code>mousedown</code></td> |
|
|
107 |
<td><code>touchstart</code></td> |
|
|
108 |
</tr> |
|
|
109 |
<tr> |
|
|
110 |
<td><strong><code>gesturemove</code></strong></td> |
|
|
111 |
<td><code>mousemove</code></td> |
|
|
112 |
<td><code>touchmove</code></td> |
|
|
113 |
</tr> |
|
|
114 |
<tr> |
|
|
115 |
<td><strong><code>gesturemoveend</code></strong></td> |
|
|
116 |
<td><code>mouseup</code></td> |
|
|
117 |
<td><code>touchend</code></td> |
|
|
118 |
</tr> |
|
|
119 |
</tbody> |
|
|
120 |
</table> |
|
|
121 |
|
|
|
122 |
<p>I say "roughly" because the gesture events aim to encapsulate common |
|
|
123 |
interactions rather than just serving as a relay to other events. Where this |
|
|
124 |
comes out is in the additional configuration that can be included in the |
|
|
125 |
subscription as a third argument.</p> |
|
|
126 |
|
|
|
127 |
<pre class="code prettyprint">// Notify me when the user puts a finger down, or mouses down on |
|
|
128 |
// the car node |
|
|
129 |
car.on("gesturemovestart", alignForMove, { |
|
|
130 |
|
|
|
131 |
// fire the event only after 300ms of continuous contact... |
|
|
132 |
minTime: 300, |
|
|
133 |
|
|
|
134 |
// ...or if the finger/mouse moves more than 3px |
|
|
135 |
minDistance: 3 |
|
|
136 |
}); |
|
|
137 |
|
|
|
138 |
// Move the car node when the user moves the finger or mouse. |
|
|
139 |
// Note the `this` override parameter is shifted to account for |
|
|
140 |
// the configuration param |
|
|
141 |
car.on("gesturemove", car.move, null, car); |
|
|
142 |
|
|
|
143 |
|
|
|
144 |
// Notify me when the user lifts their finger, or lets go of |
|
|
145 |
// the mouse button (only if a gesturemovestart was received |
|
|
146 |
// on the node). |
|
|
147 |
car.on("gesturemoveend", screechingHalt);</pre> |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
<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> |
|
|
151 |
|
|
|
152 |
<h4 id="related-events">Related Events</h4> |
|
|
153 |
|
|
|
154 |
<p>The three gesture events are related to each other. They are notifications |
|
|
155 |
for the start, progress, and end of the same gesture. <code>gesturemove</code> and |
|
|
156 |
<code>gesturemoveend</code> subscriptions won't execute unless a <code>gesturemovestart</code> |
|
|
157 |
happens.</p> |
|
|
158 |
|
|
|
159 |
<p>If you need them to fire separately, such as when attaching and detaching |
|
|
160 |
subscribers dynamically, the <code>gesturemove</code> and <code>gesturemoveend</code> events can be |
|
|
161 |
subscribed to individually by configuring <code>standAlone: true</code></p> |
|
|
162 |
|
|
|
163 |
<pre class="code prettyprint">// Doesn't require an associated `gesturemovestart` subscription |
|
|
164 |
Y.one("doc").on("gesturemove", function(e) {...}, { |
|
|
165 |
standAlone:true |
|
|
166 |
});</pre> |
|
|
167 |
|
|
|
168 |
|
|
|
169 |
<p>Under the hood, the DOM listeners which monitor mousemove/touchmove and |
|
|
170 |
mouseup/touchend are bound to the <code>document</code> by default. The node only provides |
|
|
171 |
the shared context to relate the three events.</p> |
|
|
172 |
|
|
|
173 |
|
|
|
174 |
<h2 id="flick">Flick Gesture Event</h2> |
|
|
175 |
|
|
|
176 |
<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> |
|
|
177 |
|
|
|
178 |
<pre class="code prettyprint">myNode.on("flick", function(e) { |
|
|
179 |
|
|
|
180 |
// Some of the flick specific data on the event facade |
|
|
181 |
|
|
|
182 |
var flick = e.flick, |
|
|
183 |
velocity = flick.velocity, |
|
|
184 |
distance = flick.distance, |
|
|
185 |
axis = flick.axis, |
|
|
186 |
startX = flick.start.pageX, |
|
|
187 |
startY = flick.start.pageY, |
|
|
188 |
|
|
|
189 |
// The event object itself is the event object for |
|
|
190 |
// the event which concludes the flick (the mouseup or touchend) |
|
|
191 |
|
|
|
192 |
endX = e.pageX, |
|
|
193 |
endY = e.pageY, |
|
|
194 |
endTarget = e.target; |
|
|
195 |
|
|
|
196 |
});</pre> |
|
|
197 |
|
|
|
198 |
|
|
|
199 |
<p>Like with the supporting gesture events, when subscribing to |
|
|
200 |
<code>flick</code>, you can also pass additional configuration to control |
|
|
201 |
when and how the flick subscriber is notified.</p> |
|
|
202 |
|
|
|
203 |
<pre class="code prettyprint">// Custom config, with no context or extra args |
|
|
204 |
myNode.on("flick", flickHandler, { |
|
|
205 |
|
|
|
206 |
// only notify me if the flick covered |
|
|
207 |
// more than 20px and was faster than 0.8 px/ms |
|
|
208 |
minDistance: 20, |
|
|
209 |
minVelocity: 0.8, |
|
|
210 |
|
|
|
211 |
// prevent the default behavior for the |
|
|
212 |
// underlying mouse/touch events |
|
|
213 |
preventDefault: true |
|
|
214 |
}); |
|
|
215 |
|
|
|
216 |
// Another option to avoid confusion when specifying the `this` |
|
|
217 |
// override or bound arguments for events with custom signatures |
|
|
218 |
// is to use Y.bind |
|
|
219 |
myNode.on("flick", Y.bind(o.flickHandler, o, arg1), { |
|
|
220 |
minDistance: 20, |
|
|
221 |
minVelocity: 0.8, |
|
|
222 |
preventDefault: true |
|
|
223 |
}); |
|
|
224 |
|
|
|
225 |
// Alternately, make sure to account for the additional subscription |
|
|
226 |
// parameter by passing null if there is no configuration. |
|
|
227 |
myNode.on("flick", o.flickHandler, null, o, arg1);</pre> |
|
|
228 |
|
|
|
229 |
|
|
|
230 |
<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> |
|
|
231 |
|
|
|
232 |
<h2 id="caveats">Caveats</h2> |
|
|
233 |
|
|
|
234 |
<ol> |
|
|
235 |
<li>The <code>flick</code> event doesn't (yet) support delegation.</li> |
|
|
236 |
<li> |
|
|
237 |
The <code>delegate()</code> signature for the gesture events is |
|
|
238 |
<code>node.delegate('gesturemove', callback, <em>filter</em>, |
|
|
239 |
<em>gestureConfig</em>)</code>, reversing the order of the delegation |
|
|
240 |
filter and extra params from the <a href="hover.html"><code>hover</code></a> and |
|
|
241 |
<a href="key.html"><code>key</code></a> events. This may be changed in a future |
|
|
242 |
release. |
|
|
243 |
</li> |
|
|
244 |
</ol> |
|
|
245 |
|
|
|
246 |
</div> |
|
|
247 |
</div> |
|
|
248 |
</div> |
|
|
249 |
|
|
|
250 |
<div class="yui3-u-1-4"> |
|
|
251 |
<div class="sidebar"> |
|
|
252 |
|
|
|
253 |
<div id="toc" class="sidebox"> |
|
|
254 |
<div class="hd"> |
|
|
255 |
<h2 class="no-toc">Table of Contents</h2> |
|
|
256 |
</div> |
|
|
257 |
|
|
|
258 |
<div class="bd"> |
|
|
259 |
<ul class="toc"> |
|
|
260 |
<li> |
|
|
261 |
<a href="#using-touch-events">Using Touch Events</a> |
|
|
262 |
<ul class="toc"> |
|
|
263 |
<li> |
|
|
264 |
<a href="#the-touch-event-facade">The Touch Event Facade</a> |
|
|
265 |
</li> |
|
|
266 |
</ul> |
|
|
267 |
</li> |
|
|
268 |
<li> |
|
|
269 |
<a href="#move">Cross-Device Gesture Support</a> |
|
|
270 |
<ul class="toc"> |
|
|
271 |
<li> |
|
|
272 |
<a href="#related-events">Related Events</a> |
|
|
273 |
</li> |
|
|
274 |
</ul> |
|
|
275 |
</li> |
|
|
276 |
<li> |
|
|
277 |
<a href="#flick">Flick Gesture Event</a> |
|
|
278 |
</li> |
|
|
279 |
<li> |
|
|
280 |
<a href="#caveats">Caveats</a> |
|
|
281 |
</li> |
|
|
282 |
</ul> |
|
|
283 |
</div> |
|
|
284 |
</div> |
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
<div class="sidebox"> |
|
|
289 |
<div class="hd"> |
|
|
290 |
<h2 class="no-toc">Examples</h2> |
|
|
291 |
</div> |
|
|
292 |
|
|
|
293 |
<div class="bd"> |
|
|
294 |
<ul class="examples"> |
|
|
295 |
|
|
|
296 |
|
|
|
297 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
298 |
<a href="basic-example.html">Simple DOM Events</a> |
|
|
299 |
</li> |
|
|
300 |
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed."> |
|
|
304 |
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a> |
|
|
305 |
</li> |
|
|
306 |
|
|
|
307 |
|
|
|
308 |
|
|
|
309 |
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events"> |
|
|
310 |
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a> |
|
|
311 |
</li> |
|
|
312 |
|
|
|
313 |
|
|
|
314 |
|
|
|
315 |
|
|
|
316 |
|
|
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
</ul> |
|
|
325 |
</div> |
|
|
326 |
</div> |
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
<div class="sidebox"> |
|
|
331 |
<div class="hd"> |
|
|
332 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
333 |
</div> |
|
|
334 |
|
|
|
335 |
<div class="bd"> |
|
|
336 |
<ul class="examples"> |
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
|
|
|
344 |
|
|
|
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-button.html">Accessible Menu Button</a> |
|
|
347 |
</li> |
|
|
348 |
|
|
|
349 |
|
|
|
350 |
|
|
|
351 |
<li data-description="Shows how to extend the base widget class, to create your own Widgets."> |
|
|
352 |
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a> |
|
|
353 |
</li> |
|
|
354 |
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
<li data-description="Example Photo Browser application."> |
|
|
358 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
359 |
</li> |
|
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
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> |
|
|
365 |
</li> |
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
<li data-description="Use IO to request data over HTTP."> |
|
|
370 |
<a href="../io/get.html">HTTP GET to request data</a> |
|
|
371 |
</li> |
|
|
372 |
|
|
|
373 |
|
|
|
374 |
</ul> |
|
|
375 |
</div> |
|
|
376 |
</div> |
|
|
377 |
|
|
|
378 |
</div> |
|
|
379 |
</div> |
|
|
380 |
</div> |
|
|
381 |
</div> |
|
|
382 |
|
|
|
383 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
384 |
<script>prettyPrint();</script> |
|
|
385 |
|
|
|
386 |
<script> |
|
|
387 |
YUI.Env.Tests = { |
|
|
388 |
examples: [], |
|
|
389 |
project: '../assets', |
|
|
390 |
assets: '../assets/event', |
|
|
391 |
name: 'event', |
|
|
392 |
title: 'Touch Events and Abstractions', |
|
|
393 |
newWindow: '', |
|
|
394 |
auto: false |
|
|
395 |
}; |
|
|
396 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
397 |
YUI.Env.Tests.examples.push('synth-example'); |
|
|
398 |
YUI.Env.Tests.examples.push('swipe-example'); |
|
|
399 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
400 |
YUI.Env.Tests.examples.push('widget-extend'); |
|
|
401 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
402 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
403 |
YUI.Env.Tests.examples.push('get'); |
|
|
404 |
|
|
|
405 |
</script> |
|
|
406 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
407 |
|
|
|
408 |
|
|
|
409 |
|
|
|
410 |
</body> |
|
|
411 |
</html> |