A swiping question

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
stjones
Posts: 13
Joined: Fri Dec 12, 2014 3:52 am

A swiping question

Post by stjones » Sun Dec 14, 2014 2:01 am

Trying to implement a swipe gesture for iOS. I have a stack with 9 cards. Each card has one image on it. I want to swipe left or right on a card to go to the next or previous card. I found this example swipe script in another online resource and pasted it into the script of the stack:

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

command goNext
	lock screen for visual effect
	go to next card
	unlock screen with visual effect "push left very fast"
end goNext

command goPrev
   lock screen for visual effect
   go to prev card
   unlock screen with visual effect "push right very fast"
end goPrev

When I run the script in test mode in LiveCode, it stops with an error on line 6 of the code:

if abs(the mouseH - sStartH) > 50 then

with the error message:

stack "education_images": execution error at line 6 (Operators -: range error (overflow) in array operation), char 1"

I'm still a newbie so having a hard time deciphering the error. Any suggestions?

Thanks!

stjones
Posts: 13
Joined: Fri Dec 12, 2014 3:52 am

Re: A swiping question

Post by stjones » Sun Dec 14, 2014 5:58 am

Have tried this code I found in an example as well. No errors - but nothing happens when I swipe in LiveCode, iPhone Simulator or installed on my iPhone 6. Any ideas?

Code: Select all

on touchStart pID 
   ## When the user touches the screen     
   put empty into sCoordinateArray["start"] 
   put empty into sCoordinateArray["end"]
end touchStarton touchMove pID, x y 
       if sCoordinateArray["start"] is empty then        
   put x into sCoordinateArray["start"] 
    else        
put x into sCoordinateArray["end"] 
    end if
end touchMove
on touchEnd 
    put sCoordinateArray["start"] into tStart 
    put sCoordinateArray["end"] into tEnd
## Compare the x coordinates of the start and end point     
## This tells us the direction of the movement    
 if tStart is not empty and tEnd is not empty then 
            if tStart > tEnd and tStart - tEnd > 100 then            
    moveForward 
         else if tStart < tEnd and tEnd-tStart > 100 then            
 moveBack
        end if 
    end if    
    put empty into sCoordinateArray["start"]
    put empty into sCoordinateArray["end"] 
end touchEnd

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: A swiping question

Post by Dixie » Sun Dec 14, 2014 6:57 am

stjones...

It is possible to use both the mouse commands and the touch commands to do what you want... err... but not as you have written them. I have attached a stack that will allow you to 'swipe' in the simulator or a device... but not on the desktop, as the touch commands are for mobile only...:-)

The script is in the stack script...
Attachments
swipes.livecode.zip
(2.19 KiB) Downloaded 215 times

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: A swiping question

Post by dave.kilroy » Sun Dec 14, 2014 10:03 am

Hi stjones

Dixie is quite right in that touch commands are only for mobile - but it is possible to set up swiping for desktop if you tweak the code you posted earlier:

Code: Select all

local sStartH
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
           goPrevious
       end if
   end if
end mouseUp

on goNext
   if the short name of the current cd <> "cdConfirm" then
      lock screen for visual effect in rect the uContentRect of this stack      
      go next cd
      unlock screen with visual effect "push left" very fast
   end if
end goNext

on goPrevious
   if the short name of the current cd <> "cdHome" then
      lock screen for visual effect in rect the uContentRect of this stack      
      go previous cd
      unlock screen with visual effect "push right" very fast
   end if
end goPrevious
The first thing to note that that I've declared the local variable 'sStartH' before the mousedown handler (you didn't declare yours outside a handler and this is why you were getting error messages).

Secondly your 'goNext' and 'goPrevious' commands were calling handlers that didn't exist (I created them for you in the above code) and the above works on my stack.

However you will have to do a bit of homework before using it - first figure out why I use 'cdHome' and 'cdConfirm' and change to suit your stack. And second, figure out what 'uContentRect of this stack' might be and either tweak the code so that it doesn't reference it (or for extra 'advanced' points, implement something similar on your own stack).

OR - you could just use Dixie's stack as he's probably given you the answer without any pesky homework :)

Good luck!
"...this is not the code you are looking for..."

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: A swiping question

Post by Dixie » Sun Dec 14, 2014 11:05 am

stjones...

more or less the same code, but using mouse instead of touch commands... this would work on both desktop & mobile...

Code: Select all

local hdiff
on mouseDown
      put item 1 of mouseLoc() into hdiff
end mouseDown

on mouseUp
   put item 1 of mouseLoc() into hdiffOffset
   
   if hdiff < hdiffOffset AND ABS( hdiff - hdiffOffset) > 40 then then 
      lock screen for visual effect 
      go prev card
      unlock screen with push right very fast
   end if
   
   if hdiff > hdiffOffset AND ABS( hdiff - hdiffOffset) > 40  then 
      lock screen for visual effect 
      go next card 
      unlock screen with push left very fast
   end if
end mouseUp

stjones
Posts: 13
Joined: Fri Dec 12, 2014 3:52 am

Re: A swiping question

Post by stjones » Sun Dec 14, 2014 9:55 pm

Thanks all for the help. A few things:

1) I realize touch is for mobile and mouse if for desktop. I was just trying examples I found to see if I could get it to work with either. I did see where folks had said it could be written so it would work with either - but I could get it to work with neither.

2) I tried to share here the links where I pulled the two samples from - but the system says I don't have permission to post links. One was from LiveCode University and the other from a LiveCode source at BYU.

3) I didn't realize the variable had to be declared outside the handler. I haven't done any "programming" in some time and I thought I recalled from back in using Hypercard and Supercard a local variable could be declared in the handler - only a global variable had to be declared outside. I will make note of this for the future.

4) "goNext" and "goPrev" were declared in the script - but that the sample I found declared them using "command" rather than "on". I will make note that "command" is not the proper way. I had thought that was unusual from my time with Hypercard and Supercard - but thought it was how LC implemented it.

5) "I believe the "cdHome" and "cdConfrim" script reference is to check whether the card is the first or last card in the stack and to stop the swipe from going before the first card or after the last card. I see where the last sample from Dixie (which I have tried in my stack) allows the swiping to loop either direction both before the first card and after the last card. As far as interface guidelines from Apple, is there anything regarding a series "looping" with swiping vs. having a definite first item and last item?

6) "uContentRect of this stack". Sorry - no idea.'' Any hints?

Again - thank you all very much for the help!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: A swiping question

Post by Dixie » Sun Dec 14, 2014 10:18 pm

"I believe the "cdHome" and "cdConfrim" script reference is to check whether the card is the first or last card in the stack and to stop the swipe from going before the first card or after the last card. I see where the last sample from Dixie (which I have tried in my stack) allows the swiping to loop either direction both before the first card and after the last card. As far as interface guidelines from Apple, is there anything regarding a series "looping" with swiping vs. having a definite first item and last item?
err... I don't know, but if you wanted to 'limit' the swiping but not allowing 'looping' then you would just check which card you were on in the script before the swipe...

Code: Select all

local hdiff

on mouseDown
      put item 1 of mouseLoc() into hdiff
end mouseDown

on mouseUp
   put item 1 of mouseLoc() into hdiffOffset
   
   if hdiff < hdiffOffset AND ABS( hdiff - hdiffOffset) > 40 then
      if the number of this card <> 1 then
         lock screen for visual effect 
         go prev card
         unlock screen with push right very fast
      end if
   end if
   
   if hdiff > hdiffOffset AND ABS( hdiff - hdiffOffset) > 40  then 
      if the number of this card < the number of cards then
         lock screen for visual effect 
         go next card 
         unlock screen with push left very fast
      end if
   end if
end mouseUp

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: A swiping question

Post by dave.kilroy » Sun Dec 14, 2014 10:28 pm

Hi stjones

your 1) yes touch is just for mobile but mouse works on mobile too

your 2) I think you have to have made a minimum number of posts before the system allows you include links

your 3) if you declare a local variable outside a handler it is 'visible' to all other handlers in that script - so there are 3 levels: local variable declared in a handler and 'visible' only in that handler; a local variable declared outside a handler which is 'visible' to all handlers in that script; a global variable which has to be declared in each script you need it

your 4) you can use 'on' and 'command' interchangeably (although some people think you should use 'command' for custom handlers and leave 'on' for engine level handlers)

your 5) yes quite right (no idea which way Apple thinks we should do it...)

your 6) uContentRect is a 'custom property' of the stack (check the dictionary) defining a rectangle on the card within which the visual effect is visible, and outside of which there is no visual effect while moving between cards (used if you want to keep top and bottom areas appearing to stay constant as cards move). You can use 'setprop' and 'getprop' handlers to manage custom properties (custom properties are pretty cool)
"...this is not the code you are looking for..."

Post Reply