Swipe Direction

Posted by mf, Posted on October 15, 2011

2 votes

Here is a way to detect swipe direction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local beginX 
local beginY  
local endX  
local endY 
 
local xDistance  
local yDistance  
 
function checkSwipeDirection()
 
        xDistance =  math.abs(endX - beginX) -- math.abs will return the absolute, or non-negative value, of a given value. 
        yDistance =  math.abs(endY - beginY)
        
        if xDistance > yDistance then
                if beginX > endX then
                        print("swipe left")
                else 
                        print("swipe right")
                end
        else 
                if beginY > endY then
                        print("swipe up")
                else 
                        print("swipe down")
                end
        end
        
end
 
 
function swipe(event)
        if event.phase == "began" then
                beginX = event.x
                beginY = event.y
        end
        
        if event.phase == "ended"  then
                endX = event.x
                endY = event.y
                checkSwipeDirection();
        end
end
 
Runtime:addEventListener("touch", swipe)


Replies

guh.clemente
User offline. Last seen 16 weeks 2 days ago. Offline
Joined: 14 Oct 2010

thankS

NEITAFE
User offline. Last seen 2 days 5 hours ago. Offline
Joined: 10 Sep 2011

Thanks, I added a bit to stop swipe trigger/error onload and made a minimum swipe distance check..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local beginX 
local beginY  
local endX  
local endY 
 
local xDistance  
local yDistance
 
local bDoingTouch
local minSwipeDistance = 50
local totalSwipeDistanceLeft
local totalSwipeDistanceRight
local totalSwipeDistanceUp
local totalSwipeDistanceDown
 
function checkSwipeDirection()
                if bDoingTouch == true then
                xDistance =  math.abs(endX - beginX) -- math.abs will return the absolute, or non-negative value, of a given value. 
                yDistance =  math.abs(endY - beginY)
                if xDistance > yDistance then
                        if beginX > endX then
                        totalSwipeDistanceLeft = beginX - endX
                        if totalSwipeDistanceLeft > minSwipeDistance then
                                print("Swiped Left")
                        end
                    else 
                        totalSwipeDistanceRight = endX - beginX
                        if totalSwipeDistanceRight > minSwipeDistance then
                                print("Swiped Right")
                        end
                    end
                else 
                 if beginY > endY then
                        totalSwipeDistanceUp = beginY - endY
                        if totalSwipeDistanceUp > minSwipeDistance then
                                print("Swiped Up")
                        end
                     else 
                        totalSwipeDistanceDown = endY - beginY
                        if totalSwipeDistanceDown > minSwipeDistance then
                                print("Swiped Down")
                        end
                     end
                end
        end
 end
 function swipe(event)
                if event.phase == "began" then
            bDoingTouch = true
                beginX = event.x
            beginY = event.y
        end
        if event.phase == "ended"  then
            endX = event.x
            endY = event.y
            checkSwipeDirection();
            bDoingTouch = false
        end
end
 
Runtime:addEventListener("touch", swipe)

guh.clemente
User offline. Last seen 16 weeks 2 days ago. Offline
Joined: 14 Oct 2010

thanks too!

mf
User offline. Last seen 4 weeks 5 days ago. Offline
Joined: 14 Feb 2011

Hi NEITAFE,

Thanks for those additions - great ideas !! Nice work.

Mark

EHO
User offline. Last seen 3 weeks 4 days ago. Offline
Joined: 20 Nov 2011

This looks really useful. Thanks!