Page 1 of 1

Get Left/Right finger swipes (iPad/iPhone)

Posted: Sun Jun 28, 2015 7:04 pm
by Dav
I'm trying to make a left/right finger swipe routine and thumb through pages with it. This is what I came up with so far. It needs work I think. if you would like to improve it please do. I would like to make an electronic book using sprites and think finger swipes would be nice to have for it.

The example has four pages. You can go up or down a page by swiping left and right.

- Dav

Edit: New version. Sprites now move with finger, and return place if not swiped, much like an ebook reader does it.

Code: Select all

'Swipe.txt v1.1
'Trying to Get finger swipes (left/right)
'THIS IS A WORK IN PROGRESS
'Coded by Dav, JUNE/2015

'Trying to make a left/right finger swipe routine.
'This demo has 4 pages (sprites) and you can view
'the pages by swiping left and right.


graphics
gosub makepages 'make 4 pages to test with

pg=1 'start at sprite page one

if pg=1 then leftpg=4 else leftpg = pg-1
if pg=4 then rightpg=1 else rightpg=pg+1
    
sprite str$(pg) at 0,0 'show page

button "m" text "Swipe finger left/right" at 30,30

'>>>>>>>> adjust swipe distance below <<<<>>>>>>>

SwipeDistance= screen_width()/3 'how far to swipe

do

  'if first touch..
  get touch 0 as tx,ty

  if tx>-1 then
     'while finger down,move
      while touch_x(0)>-1
        movx=touch_x(0)
        newx= movx-tx
        sprite str$(pg) at newx,0
        sprite str$(leftpg) at newx-screen_width(),0
        sprite str$(rightpg) at newx+screen_width(),0

        if newx >SwipeDistance then
          'move sprites right, get current x,y
          get sprite str$(pg) pos sx,sy
          for x = sx to screen_width() step 10
           sprite str$(pg) at x, 0
           sprite str$(leftpg) at x-screen_width(),0
           pause .002
          next x
          pg =pg-1!if pg=0 then pg=4
          goto loadpage
        end if
   
        if newx <-SwipeDistance then
          'move sprites left, get current x,y
          get sprite str$(pg) pos sx,sy
          for x = sx to -(screen_width()) step -10
           sprite str$(pg) at x, 0
           sprite str$(rightpg) at x+screen_width(),0
           pause .002
          next x
          pg =pg+1!if pg=5 then pg=1
          goto loadpage
        end if

      end while

      'if finger released before swiping enough,
      'we must reset the pages smoothly...
      get sprite str$(pg) pos sx,sy
      'go back right....
      if newx < 0 then
         for x = newx to 0 step 5
         sprite str$(pg) at x,0
         sprite str$(rightpg) at x+screen_width(),0
         pause .002
         next x
      else
        'go back left
        for x = newx to 0 step -5
        sprite str$(pg) at x,0
        sprite str$(leftpg) at x-screen_width(),0
        pause .002
        next x
      end if

      loadpage:

      leftpg=pg-1
      rightpg=pg+1
      if pg=1 then leftpg=4 
      if pg=4 then rightpg=1

      sprite str$(pg) at 0,0
      sprite str$(leftpg) at -screen_width(),0
      sprite str$(rightpg) at screen_width(),0

  end if

until 0

end


makepages:

'=== make 4 pages to use

refresh off
shadow on
for s=1 to 4
  if s=1 then graphics clear 1,0,0
  if s=2 then graphics clear 0,0,1
  if s=3 then graphics clear 0,1,0
  if s=4 then graphics clear 1,1,0
  draw font size 20
  for d= 200 to screen_height()-200 step 25
    draw text unique_str$() at 50,d
  next d
  draw font size "100"
  draw text str$(s) at screen_width()/2,50
  draw size 20
  draw rect 0,0 to screen_width(),screen_height()
  sprite str$(s) scan 0,0,screen_width(),screen_height()
  sprite str$(s) at -2000,-2000
  sprite str$(s) show
next s
graphics clear 0,0,0
refresh on

return


Re: Get Left/Right finger swipes (work in progress)

Posted: Mon Jun 29, 2015 2:16 am
by rbytes
I just tried it out, Dav, and it works well. Every so often I would swipe and not get a page change. I figured it might be that I just wasn't swiping far enough. I located the routine that senses the swipe, and changed the distance from 200 to 50 pixels. It works better for me now.

We are having a heat wave in Calgary right now, so maybe it has made me lazy!

Re: Get Left/Right finger swipes (iPad/iPhone)

Posted: Tue Jun 30, 2015 12:25 am
by Dav
Thanks ricardobytes. I had the same problem and adjusted the swipe size also. (Good luck with heat wave, we've had one too here in NC).

I have re-written the swipe routine and posted the new version above, removing the old version. This new method works much better for me and behaves more natural. The pages move with the finger now and will bounce back in place for a failed swipe, much like a real ebook reader would do. Give it a try.

You can adjust the swipe distance with the variable SwipeDistance.

- Dav

Re: Get Left/Right finger swipes (iPad/iPhone)

Posted: Tue Jun 30, 2015 1:03 am
by rbytes
Much improved! Has a more natural feel. If you set the swipe distance to a lower number (say, 100) and drag your finger a long way, you can actually scroll through all 4 pages with just one swipe. Let's see Apple beat that!