Press and hold

I’m trying to find a neat way to create and event when a user presses and holds for a couple of seconds on a UI object, which is different from just tapping.

I was thinking of creating an on touch event and creating a timer on the “began” phase and cancel the timer on the “ended” and “cancelled” events. The timer increments a counter value. For each call by that object to the handler which is not “began”, “cancelled”, or “ended” (not sure what to do with the “move” event) I check if the counter is greater than say 2 (meaning 2 seconds) and perform the action and cancel the timer.

Is this a good strategy or are there simpler way?

Thanks [import]uid: 74162 topic_id: 21569 reply_id: 321569[/import]

You can do something like this.

[lua]local startTime
local function onTouch(event)
if event.phase == “began” then
startTime = event.time
elseif event.phase == “ended” then
timeElapsed = event.time - startTime
end
end
Runtime:addEventListener(“touch”,onTouch)[/lua] [import]uid: 64174 topic_id: 21569 reply_id: 85529[/import]

I did something like this:
[blockcode]
local holdCount = 0
local holdTimer = nil
local eventTarget = nil

local function holdCountListener(event)
holdCount = holdCount + 1
if holdCount > 0 then
–do something with eventTarget
timer.cancel( holdTimer )
end
end

local function onTap(event)
if “began” == event.phase then
holdTimer = timer.performWithDelay(1000, holdCountListener, 0)
holdCount = 0
eventTarget = event
end
elseif “ended” == event.phase then
timer.cancel( holdTimer )
if holdCount > 0 then
–do somethig with eventTarget
timer.cancel( holdTimer )
else
–do something else with eventTarget
end
elseif “cancelled” == event.phase then
timer.cancel( holdTimer )
end
end
[/blockcode]

It works but I don’t like it… [import]uid: 74162 topic_id: 21569 reply_id: 85535[/import]

I’ll try this too… [import]uid: 74162 topic_id: 21569 reply_id: 85536[/import]

Here’s a tutorial: How to implement press and hold in Corona SDK.

This tutorial might be of help too:

http://coronalabs.com/blog/2014/02/04/tutorial-continuous-actions-in-corona/

Rob

Here’s a tutorial: How to implement press and hold in Corona SDK.

This tutorial might be of help too:

http://coronalabs.com/blog/2014/02/04/tutorial-continuous-actions-in-corona/

Rob