Handling a swipe gesture in a Scrolling Field...

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Handling a swipe gesture in a Scrolling Field...

Post by paulsr » Sat Sep 15, 2012 8:02 am

Hi...

I found a piece of code that handles swipe left/right gestures using mouseDown and mouseUp...

Code: Select all

on mouseDown
   put the mouseH into sStartH
end mouseDown

on mouseUp
   if abs(the mouseH - sStartH) > 50 then
       if the mouseH < sStartH then
           goNext
       else
           goPrev
       end if
   end if
end mouseUp
That seems to work fine, except...

More than one half of my card is a Scrolling Field, and the mouseDown/Up commands don't seem to be activated if I swipe within the Field.

I figured I might find some setting in the Property Inspector which would toggle the mouse behavior, but don't see anything.

Is there some way to make mouseDown/Up work inside a Scrolling Field?

Many tkx...

--paul

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Handling a swipe gesture in a Scrolling Field...

Post by jacque » Sat Sep 15, 2012 8:35 pm

The handler only manages swipes but doesn't account for any mouseups that aren't a swipe. It needs an "else" clause that deals with plain mouseups.

If you don't know how to do that, let us know and we'll help.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: Handling a swipe gesture in a Scrolling Field...

Post by paulsr » Sun Sep 16, 2012 2:39 am

jacque wrote:The handler only manages swipes but doesn't account for any mouseups that aren't a swipe. It needs an "else" clause that deals with plain mouseups.

If you don't know how to do that, let us know and we'll help.
Oh dear. It's too early in the morning for me to contemplate the concept of mouseups without mousedowns! Perhaps the mouse enters a quantum state when it's in a scrolling field and the button is both up & down until you inspect it? No. Must be a simpler explanation :)

I'm totally confused Jacqueline, so yes, please HELP...

--paul

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Handling a swipe gesture in a Scrolling Field...

Post by jacque » Sun Sep 16, 2012 3:50 am

:) Just because there's no handler for an event doesn't mean it isn't there -- it just means it isn't handled. So yes, mousedowns do happen but if you don't have a script that catches them, they do nothing. For the swipe, the mousedown event needs to record the position on screen so that the mouseup has something to compare it to. So you do need your mousedown handler, but you don't need to change anything in it.

The mouseUp handler only compares the two locations to see how far apart they are and if it's more than 50 pixels, it considers that a swipe has occured. If so, it takes action. But there are no instructions in there about what to do if it isn't a swipe, so nothing happens in that case. So all you need to do is add instructions for the instances where the mouse went up but the difference is less than 50 pixels; i.e., it's a tap, not a swipe. (By the way, I think 50 pixels is too big. I use 10 or 20 usually. It's up to you.)

So you need something like this in your mouseUp:

Code: Select all

on mouseUp
   if abs(the mouseH - sStartH) > 50 then -- it's a swipe
       if the mouseH < sStartH then
           goNext
       else
           goPrev
       end if
   else -- it's a tap
      -- do whatever you need to do here to respond to the tap in the field
   end if
end mouseUp
Your example omitted the script local variable, but nothing will work without it so make sure you include that at the top of the field script:

Code: Select all

local sStartH

on mouseDown
   put the mouseH into sStartH
end mouseDown

on mouseUp
   if abs(the mouseH - sStartH) > 50 then -- it's a swipe
       if the mouseH < sStartH then
           goNext
       else
           goPrev
       end if
   else -- it's a tap
      -- do whatever you need to do here to respond to the tap in the field
   end if
end mouseUp
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: Handling a swipe gesture in a Scrolling Field...

Post by paulsr » Sun Sep 16, 2012 4:12 am

Sorry Jacqueline, I guess I didn't explain too well...

(Yes, I do have the local var declaration, but omitted it from my sample code.)

Imagine a screen with a news article. The top of the screen has stuff like headline, date posted, an accompanying image, etc. The bottom half of the screen has the article which is displayed in a Scrolling Field (because it may not fit and therefore would need to scroll.)

The swipe gesture works fine in the top half of the screen, but is ignored if done anywhere in the bottom half, over the article text, which is in the Scrolling Field. So my question is: how can I make the swipe work when done in a scrolling field.

BTW: I'm not interested in taps, so I don't try to trap them.

Any ideas?... Tkx...

--paul

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: Handling a swipe gesture in a Scrolling Field...

Post by paulsr » Sun Sep 16, 2012 4:25 am

Just to expand on the above...

Neither the mouseDown nor mouseUp handlers is called when I swipe within the scrolling field, so there must be something different about this type of field.

HTH

--paul

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Handling a swipe gesture in a Scrolling Field...

Post by sturgis » Sun Sep 16, 2012 2:06 pm

The reason for this is that the text in the scrolling field is "editable" so the clicks are setting the insertion point in the field. Locking the field will solve this, but then the text isn't editable so a method would need to be available to handle this.

Maybe something like.. have the text locked so that taps and swipes are detected, then if its a tap, unlock the field and set the insertion point. On leaving the field, lock the text again. If it was a swipe rather than a tap, do the swipe and leave the field locked.

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: Handling a swipe gesture in a Scrolling Field...

Post by paulsr » Mon Sep 17, 2012 4:29 am

sturgis wrote:The reason for this is that the text in the scrolling field is "editable" so the clicks are setting the insertion point in the field.
Yeah, I figured it must be something like that, but couldn't find a solution. As I mentioned, I am only displaying text, I don't need it to be editable. So first, a question...

Did I pick the wrong kind of field to display the text? Is there a better way to display text that needs to scroll?
sturgis wrote:Locking the field will solve this, but then the text isn't editable so a method would need to be available to handle this.
Thank you. Very simple solution. I checked "Lock text" in the property inspector, and now everything works fine.

--paul

Post Reply