Swipe and Pinch to zoom in the same page

Hello,

I am trying to have both gestures in one of my project pages. If user swipes the background, it goes to the following page. If he/she “pinches” to zoom, the group background zooms in/out.

For the swipe, I am using the following code with success:

 local function flip (event)   
 if event.phase =="ended" then   
 if event.xStart \< event.x and (event.x - event.xStart) \>= 30 then   
 if (curPage \> 1) then   
 director:changeScene( "page\_"..curPage-1, "moveFromLeft" )   
 end   
 elseif event.xStart \> event.x and (event.xStart-event.x) \>= 30 then   
 if (curPage \< numPages) then   
 director:changeScene("page\_"..curPage+1, "moveFromRight")   
 end   
 end   
 end   
 end   
 layer1:addEventListener("touch", flip)   

My issue resides when I try to add the pinch code (extracted from the PinchZoomGesture sample app):

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  
  
 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  
 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 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  
background:addEventListener( "touch", background )  

What happens is most of times, during the pinch gesture, the pages goes to the following page as it gets confused with the flip function.

Ideas? [import]uid: 4883 topic_id: 18228 reply_id: 318228[/import]

Add some code to only allow one gesture at a time, perhaps?

It would help to provide plug and play or a sample to test - reading 120 odd lines is OK but without testing them it can be hard to envision any easy solutions.

Peach :wink: [import]uid: 52491 topic_id: 18228 reply_id: 69857[/import]