I am playing with a pan and zoom template. I figured out how to limit zooming, but how do i limit the panning of of an image. I kind of made a sloppy work around but it is bad, basically +5 to the if it goes to far left or up, or -5 it goes to far right or down. Here is the sample code.
-- -- Project: PinchZoom with Pan -- -- Date: August 19, 2010 -- -- Version: 1.1 -- -- File name: main.lua -- -- Author: Corona Labs (mikofh) -- -- Abstract: Pinch/Zoom Gesture sample app with Pan added -- -- Demonstrates: locations events, buttons, touch, Pan -- -- File dependencies: none -- -- Target devices: Devices only -- -- Limitations: Mutitouch not supported on Simulator -- -- Update History: -- v1.1 Added Simulator warning message -- -- Comments: Pinch and Zoom to scale the image on the screen. -- -- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license -- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved. --------------------------------------------------------------------------------------- -- activate multitouch system.activate( "multitouch" ) local maxpos, maypos, maxminuspos, mayminuspos = 2000, 2000, -2000, -2000 -- add bkgd image to screen local background = display.newImage( "aquariumbackgroundIPhone.jpg", 0, 0 ) local function calculateDelta( previousTouches, event ) local id,touch = next( previousTouches ) if event.id == id then id,touch = next( previousTouches, id ) assert( id ~= event.id ) end local dx = touch.x - event.x local dy = touch.y - event.y return dx, dy end -- create a table listener object for the bkgd image function background:touch( event ) local result = true local phase = event.phase local previousTouches = self.previousTouches local numTotalTouches = 1 if ( previousTouches ) then -- add in total from previousTouches, subtract one if event is already in the array numTotalTouches = numTotalTouches + self.numPreviousTouches if previousTouches[event.id] then numTotalTouches = numTotalTouches - 1 end end if "began" == phase then -- Very first "began" event if ( not self.isFocus ) then -- Subsequent touch events will target button even if they are outside the contentBounds of button display.getCurrentStage():setFocus( self ) self.isFocus = true --miko collect values for panning self.myX = event.x-self.x self.myY = event.y-self.y -- Store initial position --self.isDragging = true --miko end collection previousTouches = {} self.previousTouches = previousTouches self.numPreviousTouches = 0 elseif ( not self.distance ) then local dx,dy if previousTouches and ( numTotalTouches ) \>= 2 then dx,dy = calculateDelta( previousTouches, event ) end -- initialize to distance between two touches if ( dx and dy ) then local d = math.sqrt( dx\*dx + dy\*dy ) if ( d \> 0 ) then self.distance = d self.xScaleOriginal = self.xScale self.yScaleOriginal = self.yScale print( "distance = " .. self.distance ) end end end if not previousTouches[event.id] then self.numPreviousTouches = self.numPreviousTouches + 1 end previousTouches[event.id] = event elseif self.isFocus then if "moved" == phase then if ( self.distance ) then local dx,dy if previousTouches and ( numTotalTouches ) \>= 2 then dx,dy = calculateDelta( previousTouches, event ) end if ( dx and dy ) then local newDistance = math.sqrt( dx\*dx + dy\*dy ) local scale = newDistance / self.distance print( "newDistance(" ..newDistance .. ") / distance(" .. self.distance .. ") = scale(".. scale ..")" ) if ( scale \> 0 ) then self.xScale = self.xScaleOriginal \* scale self.yScale = self.yScaleOriginal \* scale end --miko test ind her om xScale \< 1 og så juster ved at gange op. end else self.x = event.x - self.myX self.y = event.y - self.myY if (self.x \> maxpos) then self.x = maxpos elseif (self.x \< maxminuspos) then self.x = maxminuspos end if (self.y \> maypos) then self.y = maypos elseif (self.y \< mayminuspos) then self.y = mayminuspos end end if not previousTouches[event.id] then self.numPreviousTouches = self.numPreviousTouches + 1 end previousTouches[event.id] = event elseif "ended" == phase or "cancelled" == phase then --[[if numTotalTouches == 1 then --miko move panning with one finger only local myXnew = event.x local myYnew = event.y --calculate panning left/right/up/down local mydx = math.abs(myXnew - self.myX) local mydy = math.abs(myYnew - self.myY) if (mydx \> 50) then --right/left panning mydx = myXnew - self.myX if (mydx \> 50) then transition.to(self, { time=200, x=(self.x+50) } ) --self.x = self.x+50 elseif (mydx \< -50) then transition.to(self, { time=200, x=(self.x-50) } ) end elseif (mydy \> 50) then --up/down panning mydy = myYnew - self.myY if (mydy \> 50) then transition.to(self, { time=200, y=(self.y+50) } ) elseif (mydy \< -50) then transition.to(self, { time=200, y=(self.y-50) } ) end end --move --now set variables to new x,y self.myX = event.x self.myY = event.y end]]-- if previousTouches[event.id] then self.numPreviousTouches = self.numPreviousTouches - 1 previousTouches[event.id] = nil end if ( #previousTouches \> 0 ) then -- must be at least 2 touches remaining to pinch/zoom self.distance = nil else -- previousTouches is empty so no more fingers are touching the screen -- Allow touch events to be sent normally to the objects they "hit" display.getCurrentStage():setFocus( nil ) self.isFocus = false self.distance = nil self.xScaleOriginal = nil self.yScaleOriginal = nil -- reset array self.previousTouches = nil self.numPreviousTouches = nil end end end return result end -- Determine if running on Corona Simulator -- local isSimulator = "simulator" == system.getInfo("environment") -- Multitouch Events not supported on Simulator -- if isSimulator then msg = display.newText( "Multitouch not supported on Simulator!", 0, 20, "Verdana-Bold", 14 ) msg.x = display.contentWidth/2 -- center title msg.y = display.contentHeight/2 -- center title msg:setTextColor( 255,255,0 ) end -- register table listener background:addEventListener( "touch", background )
Thanks,
Scott