iOS Question Detect finger swipe

tufanv

Expert
Licensed User
Longtime User
hello i need to detect finger swipe to right left down and up. Is it possible with b4i ?

Ty
 

ilan

Expert
Licensed User
Longtime User
try this ( i have not checked it out, it may work)

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
'....
    Dim startx As Float
    Dim starty As Float
End Sub

'......

Sub Panel1_Touch(Action As Int, X As Float, Y As Float)
  
    Select Action
        Case Panel1.ACTION_DOWN
            startx = X
            starty = Y
        Case Panel1.ACTION_MOVE
            Log("moving")
        Case Panel1.ACTION_UP
            If Abs(Y - starty) > 20%y Then Return
            If X - startx > 30%x  Then 'right
                Msgbox("Moved to the right","")
            Else If startx - X > 30%x  Then ' left
                Msgbox("Moved to the left","")
            End If
    End Select
  
End Sub
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Will check it out . Thank you Ilan !
try this ( i have not checked it out, it may work)

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
'....
    Dim startx As Float
    Dim starty As Float
End Sub

'......

Sub Panel1_Touch(Action As Int, X As Float, Y As Float)
 
    Select Action
        Case Panel1.ACTION_DOWN
            startx = X
            starty = Y
        Case Panel1.ACTION_MOVE
            Log("moving")
        Case Panel1.ACTION_UP
            If Abs(Y - starty) > 20%y Then Return
            If X - startx > 30%x  Then 'right
                Msgbox("Moved to the right","")
            Else If startx - X > 30%x  Then ' left
                Msgbox("Moved to the left","")
            End If
    End Select
 
End Sub
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Ilan , I just checked it . It is ok with the right move but it does not display moved to the left.

try this ( i have not checked it out, it may work)

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
'....
    Dim startx As Float
    Dim starty As Float
End Sub

'......

Sub Panel1_Touch(Action As Int, X As Float, Y As Float)
 
    Select Action
        Case Panel1.ACTION_DOWN
            startx = X
            starty = Y
        Case Panel1.ACTION_MOVE
            Log("moving")
        Case Panel1.ACTION_UP
            If Abs(Y - starty) > 20%y Then Return
            If X - startx > 30%x  Then 'right
                Msgbox("Moved to the right","")
            Else If startx - X > 30%x  Then ' left
                Msgbox("Moved to the left","")
            End If
    End Select
 
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
maybe it works like this?

B4X:
If x>startx and X - startx > 30%x  Then 'right
 Msgbox("Moved to the right","")
Else If x<startx and startx - X > 30%x  Then ' left
  Msgbox("Moved to the left","")
End If
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
maybe it works like this?

B4X:
If x>startx and X - startx > 30%x  Then 'right
Msgbox("Moved to the right","")
Else If x<startx and startx - X > 30%x  Then ' left
  Msgbox("Moved to the left","")
End If

tried it but still only shows right . :/
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
try this

B4X:
'....
If X - startx > 20%x  Then Msgbox("Moved to the right","") 'right
If startx - X > 20%x  Then  Msgbox("Moved to the left","") ' left
'...
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I tried ilans' code.
The principle is OK but sometimes it doesn't wotk properly, it seems that the Msgbox method introduces some trouble.
Replace the MsgBox lines by Log and it works properly.
You might perhaps reduce the 30%x value.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
ah okay . i was going crazy with the code :) will try logs ty Klaus ! and also thank you for the great "beginners guide"
I tried ilans' code.
The principle is OK but sometimes it doesn't wotk properly, it seems that the Msgbox method introduces some trouble.
Replace the MsgBox lines by Log and it works properly.
You might perhaps reduce the 30%x value.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
The principle is OK

are you sure, Klaus?

calculating a distance doesn't tell you which direction it went, does it?

or is that 20%x not the minimum distance for the swipe but the center point?

B4X:
If x>startx  Then 'right
Msgbox("Moved to the right","")
Else If x<startx  Then ' left
  Msgbox("Moved to the left","")
End If
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I tried ilans' code.
The principle is OK but sometimes it doesn't wotk properly, it seems that the Msgbox method introduces some trouble.
Replace the MsgBox lines by Log and it works properly.
You might perhaps reduce the 30%x value.

with log , it still not showing left swipe Klaus
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you look carefully at the code you'll see that once it is:
X - startx
and in the second it is:
startx - X

X - startx is positive for a right swipe and negative for a left swipe
startx - X is positive for a left swipe and negative for a right swipe

This code works, I tested it before posting !
B4X:
Private Sub pnlPage1_Touch(Action As Int, X As Float, Y As Float)
    Select Action
    Case pnlPage1.ACTION_DOWN
        startx = X
        starty = Y
    Case pnlPage1.ACTION_MOVE
'        Log("moving")
    Case pnlPage1.ACTION_UP
        If Abs(Y - starty) > 20%y Then Return
        If X - startx > 20%x  Then 'right
            Log("Moved to the right")
        Else If startx - X > 20%x  Then ' left
            Log("Moved to the left")
        End If
    End Select  
End Sub

Logs:
Application_Start
Application_Active
Moved to the right
Moved to the left
Moved to the right
Moved to the left
Moved to the right
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
try this

B4X:
'....
If X - startx > 20%x  Then Msgbox("Moved to the right","") 'right
If startx - X > 20%x  Then  Msgbox("Moved to the left","") ' left
'...

this shows right up and down as right
but not left

try the code I added in my previous post

this shows all directions as right

maybe it works like this?

B4X:
If x>startx and X - startx > 30%x  Then 'right
Msgbox("Moved to the right","")
Else If x<startx and startx - X > 30%x  Then ' left
  Msgbox("Moved to the left","")
End If

this shows right as right , up and down also as right
but not left again
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
i copied and pasted your code and got that result. İ will now try again
If you look carefully at the code you'll see that once it is:
X - startx
and in the second it is:
startx - X

X - startx is positive for a right swipe and negative for a left swipe
startx - X is positive for a left swipe and negative for a right swipe

This code works, I tested it before posting !
B4X:
Private Sub pnlPage1_Touch(Action As Int, X As Float, Y As Float)
    Select Action
    Case pnlPage1.ACTION_DOWN
        startx = X
        starty = Y
    Case pnlPage1.ACTION_MOVE
'        Log("moving")
    Case pnlPage1.ACTION_UP
        If Abs(Y - starty) > 20%y Then Return
        If X - startx > 20%x  Then 'right
            Log("Moved to the right")
        Else If startx - X > 20%x  Then ' left
            Log("Moved to the left")
        End If
    End Select 
End Sub

Logs:
Application_Start
Application_Active
Moved to the right
Moved to the left
Moved to the right
Moved to the left
Moved to the right
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
If you look carefully at the code you'll see that once it is:
X - startx
and in the second it is:
startx - X

X - startx is positive for a right swipe and negative for a left swipe
startx - X is positive for a left swipe and negative for a right swipe

This code works, I tested it before posting !
B4X:
Private Sub pnlPage1_Touch(Action As Int, X As Float, Y As Float)
    Select Action
    Case pnlPage1.ACTION_DOWN
        startx = X
        starty = Y
    Case pnlPage1.ACTION_MOVE
'        Log("moving")
    Case pnlPage1.ACTION_UP
        If Abs(Y - starty) > 20%y Then Return
        If X - startx > 20%x  Then 'right
            Log("Moved to the right")
        Else If startx - X > 20%x  Then ' left
            Log("Moved to the left")
        End If
    End Select 
End Sub

Logs:
Application_Start
Application_Active
Moved to the right
Moved to the left
Moved to the right
Moved to the left
Moved to the right

this workd fine now thank you. I was using dim startx and starty in panel panel touch event , when i moved it to globals it is fixed . Thank you all for the help !
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
this workd fine now thank you. I was using dim startx and starty in panel panel touch event , when i moved it to globals it is fixed . Thank you all for the help !

now everything is clear... :rolleyes:

i wrote in my answer that you need to decleare it in process globals...

if you do it in panel_touch event the when action_down is raised the parameters will be created and the position will be set to them and then by action_up they will be created again !!!
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
maybe it works like this?

B4X:
If x>startx and X - startx > 30%x  Then 'right
Msgbox("Moved to the right","")
Else If x<startx and startx - X > 30%x  Then ' left
  Msgbox("Moved to the left","")
End If


if x - startx > 30%x then x must be > then startx

you dont need to ask twice

its like to ask if my son is younger the me and my age - my son age is bigger then 5 ....

if my age - my son age is bigger then 5 then ofcourse my son is younger then me...
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
X - startx is positive for a right swipe and negative for a left swipe
startx - X is positive for a left swipe and negative for a right swipe

I get that part, so that 20%x is the minimal swipe distance which is not really required?
 
Upvote 0
Top