Page 1 of 1

Pinch and Zoom - iPad PDF

Posted: Mon Jun 27, 2011 12:40 am
by Tester2
Is there a way to allow a user to pinch and zoom in an iPad app with LiveCode?

I know there is RunRev lesson on multi-touch motion, but that just makes the object (square) larger or smaller - but is not actually zooming in or out.

What I need to be able to do is pinch/zoom in and out of a pdf that I am displaying.

Any help is greatly appreciated and will be rewarded with many happy emoticons!

-Tester2

Re: Pinch and Zoom - iPad PDF

Posted: Tue Jun 28, 2011 4:31 am
by MaxDangerMouse
Hi Tester,

Yeah, I know what you mean.... still trying to work this out myself.

Max

Re: Pinch and Zoom - iPad PDF

Posted: Wed Jun 29, 2011 12:40 pm
by Klaus
Hi Tester,

can't you just display the PDF in a browser object and let this native control take erm... control? :D

Read here about how to display a PDF in a browser:
http://intkeystrokes.posterous.com/
Scroll down to: VIEWING LOCAL FILE IN UIWEBVIEW IN IOS WITH LIVECODE


Best

Klaus

Re: Pinch and Zoom - iPad PDF

Posted: Wed Jun 29, 2011 4:16 pm
by Tester2
Hey Klaus,

Thanks for the link. The following is what I previously had on my card and it displayed the PDF where I could scroll up and down, but could not pinch or zoom...also it did not fill the entire screen, so there is a black bar on the right side.

Code: Select all

[
# declare the local id of the browser we are creating 
local sBrowserId

# when the card is opened make sure that default settings are configured

on preOpenCard
# quit if we are not on a mobile device
if the environment is not "mobile" then
exit preOpenCard
end if

 # create the browser
iphoneControlCreate "browser"
put the result into sBrowserId
put "file:///" & specialfolderpath("engine") & "/sample.pdf" into tLocalPDF
replace " " with "%20" in tLocalPDF

 # set up the basic defaults
iphoneControlSet sBrowserId, "rect" , the rect of group "Browser"
iphoneControlSet sBrowserId, "visible" , "true"
## Then set the iOS browser to that URL
iphoneControlSet sBrowserId, "url", tLocalPDF
end preOpenCard

on closeCard
if the environment is not "mobile" then
exit closeCard
end if

# destroy the browser we created 
iphoneControlDelete sBrowserId
end closeCard
]

I looked at the link you gave me and noticed that most of the code is the same as what I already had, with the exception of the command, CreateMyBrowser.  So I tried using the following piece of code...

[code]
[
command createMyBrowser
put rpFormatLocalFileWebView("engine","sample.pdf") into\
tMyLocalContent
iphoneControlCreate "browser"
put the result into sBrowserId
iphoneControlSet sBrowserId, "rect", the rect of graphic "Browser"
iphoneControlSet sBrowserId, "visible", "true"
iphoneControlSet sBrowserId, "autoFit", "true"
-- here where we set our content returned by the format local file for\
-- viewing in UIWebView for iOS function
iphoneControlSet sBrowserId, "url", tMyLocalContent
 end createMyBrowser]

This would not display anything...not even the PDf  :( 

Is there something I am missing?

Thanks bunches....

-Tester2

Re: Pinch and Zoom - iPad PDF

Posted: Thu Jun 30, 2011 6:46 pm
by CALL-151
Tester2,

I believe that your original code will work as you desire if you insert

iphoneControlSet sBrowserId, "autoFit", "true"

just after
iphoneControlSet sBrowserId, "visible" , "true"

This sets the scalesPageToFit property of UIWebView to true, which also allows the user to zoom. Take a look at http://developer.apple.com/library/ios/ ... rence.html

Hope that helps.

Re: Pinch and Zoom - iPad PDF

Posted: Thu Jun 30, 2011 9:50 pm
by Tester2
Call-151! OMG!! Thank you, thank you, thank you so much!! :D :D :D

You don't know how long I've been trying to be able to do that, and all it needed was that one line of code.

A million more thanks and may the best of luck come your way!

-Tester2

Re: Pinch and Zoom - iPad PDF

Posted: Thu Jul 07, 2011 4:21 pm
by Tester2
My fellow LiveCoders....

I have been up the mountain and received a gift from the gods...a stack that had the code for how to pinch and zoom on an image!!! :D

The forum won't let me upload the stack...so I'll post the long code below.

I thought it would be rude not to share....enjoy!

-Tester2

----------------------------------

Code: Select all

[
local sTouches   -- an array storing the location of all the current touches (user's fingers) on the screen
local sDist      -- if we have two tocuhes, this stores the last distance between them
local sWHRatio   -- the width height (aspect) ratio of the current image

constant kPinchScaleRatio = 0.4  -- 1 unit of pinch movement results in a kPinchScaleRatio percentage image scaling

on preOpenStack
   iphoneUseDeviceResolution true
end preOpenStack

on openCard
   
   -- cache the width height ratio of the image of this current card
   -- this is used when making sure the image fits within the current card (see validateRect)
   --
   put the effective width of image "comic" / the effective height of image "comic" into sWHRatio
   
end openCard

on touchMove pId, pX, pY
   
   if sTouches[pId] is empty then
      
      -- this is the first time we have touched, store the location of this touch
      --
      put (pX, pY) into sTouches[pId]
      
      -- if we already have another touch, then cache the distance between the two touches
      -- this means the user has placed a second finger on the screen and may be about to start a pinch motion
      -- we store the current distance between the users fingers so that we know how much the user has moved if they pinch
      --
      if the number of elements in sTouches is 2 then
         put distBetweenPts(sTouches[line 1 of the keys of sTouches], sTouches[line 2 of the keys of sTouches]) into sDist
      end if
      
   else if the number of elements in sTouches is 2 then
      
      -- store the location of this touch for future reference
      --
      put (pX, pY) into sTouches[pId]
      
      -- we have two touches, this means the user has pinched and wants to resize the image
      -- work out the distance bewtween the touches (user's fingers)
      --
      local tDist
      put distBetweenPts(sTouches[line 1 of the keys of sTouches], sTouches[line 2 of the keys of sTouches]) into tDist  
      
      -- we only want to scale the image if the distance between the two touches has changed
      -- i.e. the user has moved one or both of their fingers
      --
      if tDist is not sDist then
         
         -- work out by how much we want to scale the image based on the distance moved / pinched
         --
         local tScale
         put 1 + (tDist - sDist) / 100 * kPinchScaleRatio into tScale
         
         -- scale the image by working out its new rect
         -- the new width and height are simply calculated by multiplying by the scale
         --
         local tWidth, tHeight, tLeft, tTop
         put round(the width of image "comic" * tScale) into tWidth
         put round(the height of image "comic" * tScale) into tHeight
         
         -- the top and left are a bit trickier - we want to zoom in/out on a fixed point in the image
         -- i.e. we want the point of the image which is currently at the center of the sceen 
         -- to remain there during the scaling operation
         -- to maintain this central point we first work out the current distance between the left (and top) of the image
         -- and the center of the screen (the loc of the current card)
         -- this distance will be scaled up/down in the same way as the width and height of the image
         -- we then used this new scaled distance to calculate the new left (and top) of the image
         --
         put round(item 1 of the loc of this card + (the left of image "comic" - item 1 of the loc of this card) * tScale) into tLeft
         put round(item 2 of the loc of this card + (the top of image "comic" - item 2 of the loc of this card) * tScale) into tTop
         
         -- set the new rect of the image based on the newly calculated values
         -- making sure the rect is within the current card by calling validateRect
         -- we set the rect  rather than the width, height, left, top, loc etc as this only scales the image once
         --
         set the rect of image "comic" to validateRect((tLeft, tTop, tLeft + tWidth, tTop + tHeight), sWHRatio)
         
         -- store the new distance between the touches
         --
         put tDist into sDist
         
      end if     
      
   else
      
      -- if the user is not pinching/scaling the image then we just want to move the image
      -- calculate by how much we want to move the image based on how far the user has moved thier finger
      -- this is done by calculating the difference between the current touch x and y coords from the last
      --
      local tXOffset, tYOffset
      put pX - item 1 of sTouches[pId] into tXOffset
      put pY - item 2 of sTouches[pId] into tYOffset
      
      -- set the new position of the image remebering to make sure it is within the current card
      --
      set the rect of image "comic" to validateRect((the left of image "comic" + tXOffset, the top of image "comic" + tYOffset, the right of image "comic" + tXOffset, the bottom of image "comic" + tYOffset), sWHRatio)
      
      -- store the location of the touch so we can calculate the distance on the next move or pinch
      --
      put (pX, pY) into sTouches[pId]
      
   end if
   
end touchMove

-- on touch end and touch release remove the touch location data from our array of touches 

on touchEnd pId
   delete variable sTouches[pId]
end touchEnd

on touchRelease pId
   delete variable sTouches[pId]
end touchRelease

-- Returns the distance between two coordinates using Pythagoras theorem
--
private function distBetweenPts pPt1, pPt2
   return sqrt((item 1 of pPt1 - item 1 of pPt2) ^ 2 + (item 2 of pPt1 - item 2 of pPt2) ^ 2)
end distBetweenPts

-- Make sure the rect passed is not outwith the bounds of the current card
-- Returns a new rect that fits within the bounds of the current card
--
private function validateRect pRect, pWHRatio
   
   -- if the width of the rect is less than the width of this card
   -- then scale the rect so that it's equal to the width of the card
   -- as we are altering the width of the rect, we want to maintain its aspect ratio
   -- so also set the height based of the width height ratio passed
   --
   if (item 3 of pRect - item 1 of pRect) < the width of this card then
      put item 1 of pRect + the width of this stack into item 3 of pRect
      put item 2 of pRect + the width of this stack div pWHRatio into item 4 of pRect
   end if 
   
   -- do the same if the rect is taller than the current card
   -- set the rect to be the same height as the stack and scale the width appropriately
   --
   if (item 4 of pRect - item 2 of pRect) < the height of this card then
      put item 2 of pRect + the height of this card into item 4 of pRect
      put item 1 of pRect + the height of this card * pWHRatio into item 3 of pRect
   end if 
   
   -- make sure that there is no space around the outside of the rect
   -- for example, if there is space bewteen the left hand side of this card and the rect
   -- (the left of the rect > 0), then snap the left of the rect to the left hand side of this card
   -- remembering to adjust the right of the rect so that the width is preserved
   --  
   if item 1 of pRect > 0 then
      put (0, item 2 of pRect, item 3 of pRect - item 1 of pRect, item 4 of pRect) into pRect 
   end if
   if item 2 of pRect > 0 then
      put (item 1 of pRect, 0, item 3 of pRect, item 4 of pRect - item 2 of pRect) into pRect 
   end if
   if item 3 of pRect < the width of this card then
      put (the width of this card - (item 3 of pRect - item 1 of pRect), item 2 of pRect, the width of this card, item 4 of pRect) into pRect 
   end if
   if item 4 of pRect < the height of this card then 
      put (item 1 of pRect, the height of this card - (item 4 of pRect - item 2 of pRect) , item 3 of pRect, the height of this card) into pRect 
   end if 
   
   -- return the adjusted rect
   --
   return pRect
   
end validateRect
]

Re: Pinch and Zoom - iPad PDF

Posted: Thu Jul 07, 2011 4:39 pm
by Klaus
Hi all,

there is a "Pinch" lesson somewhere on the RunRev site.
It uses a graphic, but when you change this to an image it will also work :-)
http://lessons.runrev.com/spaces/lesson ... ch-motion-


Best

Klaus

Re: Pinch and Zoom - iPad PDF

Posted: Mon Oct 03, 2011 1:46 am
by FireWorx
Hi,
My IOS iPad App loads PDF pages into the safari browser window and I use the auto fit property. When I run the App on my iPad devise the zoom level although pretty good doesn't go as deep as when I bring the same PDF up in Ibooks. Also Ibooks refreshes faster and locks up less.

The idea is to be able to pull up a map page and zoom all the way in until you can see an address in an apartment complex. Safari doesn't go quite deep enough. I checked the IOS development library for Safari and didn't se any settings for the zoom level like you might see in the google maps API.

Any suggestions? I am considering:
1) Disabling auto fit and trying to use the pinch zoom code provided in the tutorial. It seems to go in very very deep on a graphic.
2) Researching to se if in fact there are settings that can be tweaked for performance. "constant buffer I think I heard ?!?" huh
3) Trying to open the PDF's in I books?
4) copying the PDF and loading it into a graphic and allowing the user to zoom in on the graphic then dumping the graphic after?

Any thoughts would be much appreciated.
Dave