[RESOLVED] Cancel touch on sliding off button

This has been asked numerous times and each time it seems to come down to a bevy of answers. The code is an attempt to implement a number of sliding widget buttons. The touch listener is attached to the background as well as to the individual buttons using onEvent. They all call the following function:
[lua]local function backgroundTouch(event)
local self = event.target
if event.phase == “began” or event.phase == “press” then
buttonGroup.xStart = buttonGroup.x
display.getCurrentStage():setFocus(self)
self.isFocus = true
elseif self.isFocus == true then
if event.phase == “moved” then
local delta = event.xStart - event.x
buttonGroup.x = buttonGroup.xStart - delta
–Move all objects in a display group along the x-axis
elseif event.phase == “ended” or
event.phase == “release” or
event.phase == “cancelled” then
–Do some clean up that never happens
display.getCurrentStage():setFocus(nil)
self.isFocus = false
end
end
return true
end[/lua]

If the user touches the background, all events are received properly. If the user touches and drags the button, events occur properly unless the user drags their finger up or down until they are ‘off’ the button. In that case, the ‘ended’ or ‘release’ phase never occurs. So: drag left/right and end on button, no problem; drag diagonal and end off button, no ‘release’ phase.

–Resolution here
When the user slides a finger off the button and onto the background, change the focus of the touch event and start sending new events to the listener. This allows the app to seamlessly transition from ‘buttonEvent’ to ‘touch’ event. Simply check if the event.y value is outside a given set of boundaries and dispatch a new event.

[lua]local function backgroundTouch(event)
local self = event.target
if event.phase == “began” or event.phase == “press” then
buttonGroup.xStart = buttonGroup.x
display.getCurrentStage():setFocus(self)
self.isFocus = true
elseif self.isFocus == true then
if event.phase == “moved” then
local delta = event.xStart - event.x
buttonGroup.x = buttonGroup.xStart - delta
–Move all objects in a display group along the x-axis
if event.y > 275 or event.y < 45 then
display.getCurrentStage():setFocus(background)
self.isFocus = false
background.isFocus = true
background:dispatchEvent({name=touch, phase=“moved”})
end
elseif event.phase == “ended” or
event.phase == “release” or
event.phase == “cancelled” then
display.getCurrentStage():setFocus(nil)
self.isFocus = false
end
end
return true
end[/lua]

It is necessary to forward declare the background variable to make this work so do keep variable scope in mind. Hope this helps someone else! [import]uid: 168249 topic_id: 31071 reply_id: 331071[/import]