x


Distinguish between a tap and a hold on iOS

Just curious, but how do you distinguish between a tap and a hold on the iPhone. Just looking at the different keywords we have, I thought it might have something to do with TouchPhase.Began and TouchPhase.Stationary, but nothing I've tried works.

I'm trying to set up a timer through the update function that says GetMouseButton, timer += Time.deltaTime and then once the timer exceeds a certain value (0.7s) then it will be considered a hold but I can't find a similar function for GetMouseButton via the Touch controls.

Whatever method I use, it will need to coexist with flick and double tap controls as well. :P

Any suggestions or links to good iphone control resources?

This is the code that I'm juggling at the moment.

void Update()
{
  if(Input.touchCount > 0)
  {
     //start timer
     holdCount += Time.deltaTime;
     if(holdCount > 0.7f)
     {
        //input counts as a hold
        holdState = true;
        //User releases hold (works but reluctant to use as I've heard TouchPhase.Ended gets skipped a lot)
        if (Input.GetTouch(0).phase == TouchPhase.Ended)
        {
           //holdStuff happens & reset
           holdState = false;
           holdCount = 0;
           Debug.Log("Hold finished")
        }
     }
      else
        //input counts as tap
        if ((Input.GetTouch(0).phase == TouchPhase.Ended) && (holdState == false))
        {
           Debug.Log("Instantiate object");
           holdCount = 0;
        }
     }
}
more ▼

asked Nov 10 '11 at 08:06 AM

BlacKatFever gravatar image

BlacKatFever
101 22 26 27

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I handle all touch stuff in an Update. Keep track of when a touch starts, and if it's held past a certain amount of time, it's a 'long click'. Similar for double-tap, looking for touch begin/end and time between last end and next begin.

more ▼

answered Nov 10 '11 at 08:18 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

Thanks for the quick reply. I'm hesitant to use TouchPhase.states because of this post on the Unity forums.

http://forum.unity3d.com/threads/93987-Unity-remote-and-touch-phase-ended

Have you had any problems with this yourself?

Nov 10 '11 at 08:30 AM BlacKatFever
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3
x1

asked: Nov 10 '11 at 08:06 AM

Seen: 1446 times

Last Updated: Nov 10 '11 at 09:09 AM