04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12: Events<br />

These properties can be used to track the touch around the screen. For example:<br />

function handleTouchEvent(event){<br />

//only for one touch<br />

if (event.touches.length == 1)<br />

}<br />

}<br />

var output = document.getElementById(“output”);<br />

switch(event.type){<br />

case “touchstart”:<br />

output.innerHTML = “Touch started (“ + event.touches[0].clientX +<br />

“,” + event.touches[0].clientY + “)”;<br />

break;<br />

case “touchend”:<br />

output.innerHTML += “ < br > Touch ended (“ +<br />

event.changedTouches[0].clientX + “,” +<br />

event.changedTouches[0].clientY + “)”;<br />

break;<br />

case “touchmove”:<br />

event.preventDefault(); //prevent scrolling<br />

output.innerHTML += “ < br > Touch moved (“ +<br />

event.changedTouches[0].clientX + “,” +<br />

event.changedTouches[0].clientY + “)”;<br />

break;<br />

}<br />

EventUtil.addHandler(document, “touchstart”, handleTouchEvent);<br />

EventUtil.addHandler(document, “touchend”, handleTouchEvent);<br />

EventUtil.addHandler(document, “touchmove”, handleTouchEvent);<br />

This code tracks a single touch around the screen. To keep things simple, it outputs information only<br />

when there ’ s a single active touch. When the touchstart event occurs, it outputs the location of the<br />

touch into a < div > . When a touchmove event fires, its default behavior is canceled to prevent scrolling<br />

(moving touches typically scroll the page) and then it outputs information about the changed touch.<br />

The touchend event outputs the last information about the touch. Note that there is nothing in the<br />

touches collection during the touchend event because there is no longer an active touch; the<br />

changedTouches collection must be used instead.<br />

These events fire on all elements of the document, so you can manipulate different parts of the page<br />

individually.<br />

Gesture Events<br />

The iPhone 2.0 version of Safari also introduced a class of events for gestures. A gesture occurs when two<br />

fingers are touching the screen and typically causes a change in the scale of the displayed item or the<br />

rotation. There are three gesture events, as described here:<br />

420<br />

❑<br />

❑<br />

❑<br />

gesturestart — Fires when a finger is already on the screen and another finger is placed on<br />

the screen<br />

gesturechange — Fires when the position of either finger on the screen has changed<br />

gestureend — Fires when one of the fingers has been removed from the screen

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!