Help with touchmove

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 489
Joined: Thu Sep 04, 2008 6:23 am

Help with touchmove

Post by jameshale » Mon May 12, 2025 3:49 am

Hi,
I am trying to enable a swipe to the right on a locked field to activate an action.
I have used the following code:

Code: Select all

local sLastX, pID

on touchStart pTouchData
   put pTouchData into pID
end touchStart

on touchMove pTouchID, pTouchX, pTouchY
   put pTouchID into pID
   -- sLastX[pTouchID] will be empty when the first move
   -- message for pTouchID is sent
   if sLastX[pTouchID] is empty then
      put pTouchX into slastX[pID]
   end if
   if pTouchX > (slastX[pID] +10) then 
      swipeRight
      breakpoint
   end if
end touchMove

on touchEnd pTouchData
   delete variable slastX[pID]
end touchEnd

Nothing happens when I try this in the simulator.
Not even the breakpoint is activated (although that might be an issue with breakpoints)
I guess I am having trouble understanding the touchmove message.
There are examples of pinch decoding given in lessons and the forum but no examples of a simple swipe.

Can anyone help my aged brain with this?

James

andresdt
Posts: 156
Joined: Fri Aug 16, 2019 7:51 pm
Contact:

Re: Help with touchmove

Post by andresdt » Mon May 12, 2025 4:19 pm

He modified your code a bit:

Code: Select all

local sLastX, sTouchID

on touchStart pTouchID
	/* Stores the id of the first touch, since each touch generates an id */
	if sTouchID is not an integer then
		put pTouchID into sTouchID
	end if
end touchStart

on touchMove pTouchID, pTouchX, pTouchY
	if pTouchID is not sTouchID then
		exit touchMove -- or  pass "touchMove"
	end if

	if sLastX[pTouchID] is empty then
		put pTouchX into sLastX[pTouchID]
	else if 10 < abs(sLastX[pTouchID] - pTouchX) then 
		swipeRight
		breakpoint
	end if
end touchMove

on touchEnd pTouchData
	delete variable sLastX[pTouchID]
end touchEnd
I hope this helps.
I can also recommend that you take a look at the extension:
https://livecode.com/extensions/mobile-gestures/1-0-0/
The source code for that extension is at:
https://github.com/Ferruslogic/touchGestures
I hope this can help you in some way.
Be kind, we all have our own wars.
https://torocruzand.com/

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 489
Joined: Thu Sep 04, 2008 6:23 am

Re: Help with touchmove

Post by jameshale » Tue May 13, 2025 2:57 am

Thanks.
Interestingly it works once, that's it.
One swipe and then no more.
??

Will explore further.

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 489
Joined: Thu Sep 04, 2008 6:23 am

Re: Help with touchmove

Post by jameshale » Tue May 13, 2025 7:57 am

Getting each handler to write to a field shows me:
touchStart is triggered
touchMove is not
Tested in Simulator AND my iPad
??

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 489
Joined: Thu Sep 04, 2008 6:23 am

Re: Help with touchmove

Post by jameshale » Wed May 14, 2025 6:47 am

OK Got it working.
I realised that as I only wanted to act at the end of the swipe putting my decision tree in the TouchMove handler would be confusing, given that it continues to be called during the swipe.
So I placed it in the TouchEnd where I want the action triggered.

Code: Select all

local sLastX,sLastY, sTouchID,sfinalx,sfinaly

on touchStart pTouchID
   /* Stores the id of the first touch, since each touch generates an id */
   if sTouchID is not an integer then
      put pTouchID into sTouchID
   end if
end touchStart

on touchMove pTouchID, pTouchX, pTouchY
   if sLastX[pTouchID] is empty then
      put pTouchX into sLastX[pTouchID]	
      put pTouchY into sLastY[pTouchID]	
   end if
   put pTouchX into sfinalx
   put pTouchY into sfinalY
end touchMove

on touchEnd pTouchID
   --   if (sLastY[pTouchID] +10) < sfinalY then
   --      -- code
   --      swipeUP
   --   else if (sLastY[pTouchID] +10) > sfinalY then
   --      -- code
   --      swipeDown
   if (sLastX[pTouchID] +10) < sfinalx then
      -- code
      swipeRight
   else if (sLastX[pTouchID] +10) > sfinalx then
      -- code
      swipeleft
   end if
   delete variable sLastX[pTouchID]
end touchEnd
shipwright and swipe left ad handlers to do what I want,

One curious thing though, you will notice I had a swipeUp and SwipeDown handler that are now commented out.
What ever value for the condition I entered, the if statement would only ever do the Up (only the first time) or Down actions, no matter which way I swiped.
I did view the appropriate values and it should have worked, but alas no.
Given I really only wanted the left and right swipes (up and down were just bonuses) I am simply happy it worked.

Post Reply