@OderWat
Here is my modified code. It does support pinch/zoom and single finger movement of the image. It also doesn’t require both fingers touch the screen at the same time.
The code is not totally complete because it doesn’t check for screen bounds (for image movement) and there is a bug where the image jumps position when touched after movement.
Tom
[code]
– activate multitouch
system.activate( “multitouch” )
– add bkgd image to screen
local background = display.newImage( “aquariumbackgroundIPhone.jpg”, 0, 0 )
– create a table listener object for the bkgd image
function background:touch( event )
local result = true
local phase = event.phase
txtTouchPhase.text = "touch Phase: " … event.phase
– when multitouch is active, the event will contain an array of
– all touch events that have changed since the last touch event
– these should all share the same phase
local touches = event.touches
– when multitouch is active, the event will contain an array of
– all stationary touch events since the last touch event
local previousTouches = event.previousTouches
local numTotalTouches = #touches
if ( previousTouches ) then
numTotalTouches = numTotalTouches + #previousTouches
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 stageBounds of button
display.getCurrentStage():setFocus( self )
self.isFocus = true
end
if ( not self.distance ) then
local dx,dy
if #touches >= 2 then
dx = touches[2].x - touches[1].x
dy = touches[2].y - touches[1].y
elseif previousTouches and ( numTotalTouches ) >= 2 then
dx = previousTouches[1].x - touches[1].x
dy = previousTouches[1].y - touches[1].y
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
elseif self.isFocus then
if “moved” == phase then
if ( self.distance ) then
local dx,dy
if #touches >= 2 then
dx = touches[2].x - touches[1].x
dy = touches[2].y - touches[1].y
elseif previousTouches and ( numTotalTouches ) >= 2 then
dx = previousTouches[1].x - touches[1].x
dy = previousTouches[1].y - touches[1].y
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
else – added
– ****** new code 6/7/10
– This fixes the problem where the second finger touches the screen after
– the “began” phase. Before this code was added you could only pinch and
– zoom if both fingers were touched at the same time.
local dx,dy
if #touches >= 2 then
dx = touches[2].x - touches[1].x
dy = touches[2].y - touches[1].y
elseif previousTouches and ( numTotalTouches ) >= 2 then
dx = previousTouches[1].x - touches[1].x
dy = previousTouches[1].y - touches[1].y
else
– Single Touch Moved
self.x = self.x + touches[1].x - self.x
self.y = self.y + touches[1].y - self.y
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 of new code
end
elseif “ended” == phase or “cancelled” == phase then
– if there are no previous touches, then there no more fingers will touch the screen
if ( not previousTouches ) then
– 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
elseif ( #previousTouches == 1 ) then
– must be at least 2 touches remaining to pinch/zoom
self.distance = nil
end
end
end
return result
end
txtTouchPhase = display.newText( “touch Phase: begin”, 45, 440, “Verdana-Bold”, 16 )
txtTouchPhase:setTextColor( 255,255,255 )
– register table listener
background:addEventListener( “touch”, background )
[/code] [import]uid: 6119 topic_id: 1176 reply_id: 3217[/import]