Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement swipe gestures for mobile devices?

I have an application made in AngularJS which has arrow key navigation to switch views.

I want to implement this navigation using swipe for touch devices. I tried jGestures library but it doesn't go well with swipe. I have been recommended NOT to use jquery mobile.

Is there any other way to implement swipe?

EDIT: It does not detect swipe cleanly. I tested it on multiple devices, including iPad and it takes multiple swipes to do an action(routing in my case).

like image 218
fotuzlab Avatar asked Feb 26 '13 08:02

fotuzlab


People also ask

How do you implement a swipe?

To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout as the parent of a single ListView or GridView . Remember that SwipeRefreshLayout only supports a single ListView or GridView child. You can also use the SwipeRefreshLayout widget with a ListFragment .

How do I change my Android to swipe gestures?

Scroll down to the Interaction Controls section and tap System controls. Tap System navigation. Tap 3-button navigation or Gesture navigation. Tap the Settings button next to Gesture navigation to change the sensitivity of the screen to the Back swipe gesture.


2 Answers

I made this function for my needs.

Feel free to use it. Works great on mobile devices.

function detectswipe(el,func) {   swipe_det = new Object();   swipe_det.sX = 0; swipe_det.sY = 0; swipe_det.eX = 0; swipe_det.eY = 0;   var min_x = 30;  //min x swipe for horizontal swipe   var max_x = 30;  //max x difference for vertical swipe   var min_y = 50;  //min y swipe for vertical swipe   var max_y = 60;  //max y difference for horizontal swipe   var direc = "";   ele = document.getElementById(el);   ele.addEventListener('touchstart',function(e){     var t = e.touches[0];     swipe_det.sX = t.screenX;      swipe_det.sY = t.screenY;   },false);   ele.addEventListener('touchmove',function(e){     e.preventDefault();     var t = e.touches[0];     swipe_det.eX = t.screenX;      swipe_det.eY = t.screenY;       },false);   ele.addEventListener('touchend',function(e){     //horizontal detection     if ((((swipe_det.eX - min_x > swipe_det.sX) || (swipe_det.eX + min_x < swipe_det.sX)) && ((swipe_det.eY < swipe_det.sY + max_y) && (swipe_det.sY > swipe_det.eY - max_y) && (swipe_det.eX > 0)))) {       if(swipe_det.eX > swipe_det.sX) direc = "r";       else direc = "l";     }     //vertical detection     else if ((((swipe_det.eY - min_y > swipe_det.sY) || (swipe_det.eY + min_y < swipe_det.sY)) && ((swipe_det.eX < swipe_det.sX + max_x) && (swipe_det.sX > swipe_det.eX - max_x) && (swipe_det.eY > 0)))) {       if(swipe_det.eY > swipe_det.sY) direc = "d";       else direc = "u";     }      if (direc != "") {       if(typeof func == 'function') func(el,direc);     }     direc = "";     swipe_det.sX = 0; swipe_det.sY = 0; swipe_det.eX = 0; swipe_det.eY = 0;   },false);   }  function myfunction(el,d) {   alert("you swiped on element with id '"+el+"' to "+d+" direction"); } 

To use the function just use it like

detectswipe('an_element_id',myfunction);  detectswipe('an_other_element_id',my_other_function); 

If a swipe is detected the function "myfunction" is called with parameter element-id and "l,r,u,d" (left,right,up,down).

Example: http://jsfiddle.net/rvuayqeo/1/


I (UlysseBN) made a new version of this script based on this one which use more modern JavaScript, it looks like it behaves better on some cases. If you think it should rather be an edit of this answer let me know, if you are the original author and you end up editing, I'll delete my answer.

like image 114
EscapeNetscape Avatar answered Sep 23 '22 02:09

EscapeNetscape


Have you tried Hammerjs? It supports swipe gestures by using the velocity of the touch. http://eightmedia.github.com/hammer.js/

like image 35
Jorik Avatar answered Sep 20 '22 02:09

Jorik