Support => NGUI 3 Support => Topic started by: Shifty Geezer on October 09, 2014, 09:30:04 AM
Title: How to implement LongPress?
Post by: Shifty Geezer on October 09, 2014, 09:30:04 AM
I've been using OnPress() events for button IO. I now want a long-press, so if the button is held down for longer than a period of time, something happens.
I can use a timer started by OnPress() and on update, accumulate time since it was pressed and test against a threshold. This works except it doesn't register a movement off the button/touch place as a cancel, so a user can press a button, move their finger elsewhere, and the hold still triggers.
How can I test if the finger hasn't moved off the button after touch down?
Note I'm not just using this with buttons with colliders, but also the whole level to set points on the map. In effect, the whole screen is a UI.
Title: Re: How to implement LongPress?
Post by: ArenMook on October 10, 2014, 02:27:52 AM
void OnPress (bool isPressed)
{
if(isPressed) Invoke("LongPress", 2f);
else CancelInvoke("LongPress");
}
void OnDragOut (){ CancelInvoke("LongPress");}
void LongPress ()
{
Debug.Log("Long press!");
}
Title: Re: How to implement LongPress?
Post by: UNSH on April 01, 2015, 06:52:33 AM
For anybody else arriving here. I had to stop the coroutine with strings or the stopcoroutine wouldn't work. My knowledge of coroutines isn't deep enough to give the why's or even if it's good practice, but here you go.