Saturday, 5 December 2020

How to do Tap and Touch gestures in PhoneGap?

 In our previous section, we learned about the hammer library and how we can detect the gesture in PhoneGap. In this section we will learn about the tap and touch gestures. We will take a closer look at how these gestures work. We will use our previous example and make some changes in it for tap and touch gestures. These are the following steps used to develop tap and touch gesture application:

1) Create index2.html

We will create a new file index2.html with the same code as preset in the index.html. We will make changes later in index2.html file not in index.html.

Tap and Touch gestures in PhoneGap
Tap and Touch gestures in PhoneGap

3) Getting the gesture div

Now, we will get the gesture div by using id and create the hammer manager object. The hammer manager object doesn't have any pre saved event. So, we need to define the event on our own.

  1. var myElement = document.getElementById('gestureDiv');  
  2. var mc = new Hammer.Manager(myElement);  

3) Adding Event

Now, we will add single tap and double tap events because the hammer manager object doesn't have any pre saved event. We will use the add function of the hammer manager and pass the event which we want to add. We will add the events in the following way:

  1. //Events Hammer will detect  
  2. mc.add(new Hammer.Tap({ event: 'doubletap', taps: 2 }));  
  3. mc.add(new Hammer.Tap({ event: 'singletap' }));  

4) Attach both the events

Now, we will attach or coordinate both the events with each other because when we tap once that is already in event, we can never get to the double tap unless we made these two events work together. We will attach both the events using the get function of the hammer manager in the following way:

  1. //Make them work together  
  2. mc.get('doubletap').recognizeWith('singletap');  
  3. mc.get('singletap').requireFailure('doubletap');  

5) Detect the event

When we press the single tap or double tap, the anonymous function will be called. In this function, we will set the event type to our result div in the following way:

  1. mc.on("singletap doubletap", function(ev){  
  2.         document.getElementById("resultDiv").innerHTML = ev.type;  
  3. })  

Now, we will run our application on PhoneGap Developer App or web using local host.

Complete Code

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset"utf-8" >  
  5.         <meta name = "viewport" content = "initial-scale = 1, maximum-scale = 1, user-scalable = no, width = device-width" >  
  6.         <script src = "hammer.js" ></script>  
  7.         <script src = "hammertime.js" ></script>  
  8.         <style>  
  9.         #gestureDiv  
  10.             {  
  11.                 width : 300px;  
  12.                 height : 400px;  
  13.                 background-color : red;  
  14.                 border : 1px solid black;  
  15.             }  
  16.         </style>  
  17.         <title> Gesture Example </title>  
  18.     </head>  
  19.     <body>  
  20.         <div id = "gestureDiv" ></div>  
  21.         <div id = "resultDiv" ></div>  
  22.         <script type = "text/javascript" src = "cordova.js" ></script>  
  23.         <script>  
  24.           var myElement = document.getElementById('gestureDiv');  
  25.           var mc = new Hammer.Manager(myElement);  
  26.             
  27.           //Events Hammer will detect  
  28.           mc.add(new Hammer.Tap({event : 'doubletap', taps : 2}));  
  29.           mc.add(new Hammer.Tap({event : 'singletap'}));  
  30.               
  31.           //Make them work together  
  32.           mc.get('doubletap').recognizeWith('singletap');  
  33.           mc.get('singletap').requireFailure('doubletap');  
  34.               
  35.             
  36.           mc.on("singletap doubletap", function(ev){  
  37.               document.getElementById("resultDiv").innerHTML = ev.type;  
  38.           })  
  39.         </script>  
  40.     </body>  
  41. </html>  

Output

Tap and Touch gestures in PhoneGap













No comments:

Post a Comment