Come da titolo, vorrei intercettare la posizione della pressione del dito su un determinato object (panel o canvas) come devo comportarmi, cosa devo usare per ottenere questo controllo.
Dai un occhio a questo topic.
http://www.b4x.com/forum/basic4android-updates-questions/15764-get-touch-position-views-consume-touch-event.html#post89540
C'è un esempio.
if x = 10 and <= 200 then
if deltaY.value > 0 then
fai
else
fai
end if
end if
Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
Select Action
Case Activity.ACTION_DOWN
LastY = Y
Case Activity.ACTION_MOVE
If Abs(Y - LastY) > 0 Then
index = index + 1
If index > value1.Length-1 Then
index = 0
End If
Else
index = index-1
If index < 0 Then
index = 0
End If
End If
scrollValue
Case Activity.ACTION_UP
End Select
Return True
End Sub
E' molto simile al codice dove ti ho linkato io :sign0161:
Ciao Ragazzi della ML;
Questo codice sembra andare quasi bene, ma funziona solo se muovo il dito in orizzontale e non verticale
B4X:Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event Select Action Case Activity.ACTION_DOWN LastY = Y Case Activity.ACTION_MOVE If Abs(Y - LastY) > 0 Then index = index + 1 If index > value1.Length-1 Then index = 0 End If Else index = index-1 If index < 0 Then index = 0 End If End If scrollValue Case Activity.ACTION_UP End Select Return True End Sub
Dove sbaglio!!!! :BangHead:
Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
Select Action
Case Activity.ACTION_DOWN
LastY = Y
Case Activity.ACTION_MOVE
If Abs(Y - LastY) > 0 Then
index = index + 1
If index > value1.Length-1 Then
index = 0
End If
Else
index = index-1
If index < 0 Then
index = 0
End If
End If
scrollValue
Case Activity.ACTION_UP
End Select
Return True
End Sub
Sub Globals
Dim Panel1 As Panel
Dim Label1 As Label
Dim X0,Y0,X1,Y1 As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
Panel1.Initialize("Panel1")
Activity.AddView(Panel1,0,0,100%x,100%y)
Label1.Initialize("")
Activity.AddView(Label1,0,0,100%x,10%y)
End Sub
Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean
Select Action
Case Activity.ACTION_DOWN
X0 = X
Y0 = Y
Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
Case Activity.ACTION_MOVE
X1 = X-X0
Y1 = Y-Y0
Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
Case Activity.ACTION_UP
Label1.Text=""
End Select
End Sub
C'è una cosa che non ho capito , ti servono le coordinate xy esatte in cui punti il dito sul panel e durante il movimento, oppure altro?
Con questo codice le coordinate ti vengono date su entrambe le coordinate.
B4X:Sub Globals Dim Panel1 As Panel Dim Label1 As Label Dim X0,Y0,X1,Y1 As Int End Sub Sub Activity_Create(FirstTime As Boolean) Panel1.Initialize("Panel1") Activity.AddView(Panel1,0,0,100%x,100%y) Label1.Initialize("") Activity.AddView(Label1,0,0,100%x,10%y) End Sub Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean Select Action Case Activity.ACTION_DOWN X0 = X Y0 = Y Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1 Case Activity.ACTION_MOVE X1 = X-X0 Y1 = Y-Y0 Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1 Case Activity.ACTION_UP Label1.Text="" End Select End Sub
Strano che non ti dia il valore y.
Inviato da mio Samsung Galaxy S II
Il mio esempio funzione in ogni direzione, se all'interno della tua app non funzione c'è un altro fattore che ignoro.
Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean
Select Action
Case Activity.ACTION_DOWN
X0 = X
Y0 = Y
Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
Case Activity.ACTION_MOVE
X1 = X-X0
Y1 = Y-Y0
' modifica di ivano monti
If Abs(Y1 - Y-Y0) > 40 Then
index = index + 1
If index > value1.Length-5 Then
index = 0
End If
Else If Abs(Y1 - Y-Y0) < -40 Then
index = index-1
If index < 0 Then
index = 0
End If
End If
scrollValue
' fine modifica di ivano monti
Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
Case Activity.ACTION_UP
Label1.Text=""
End Select
Return True
End Sub
Sub Panel1_LongClick
Label1.Text = Result(2)
Msgbox(Result(2),"Select item")
End Sub
Sub Panel1_Click
Label1.Text = Result(2)
Msgbox(Result(2),"Select item")
End Sub