- Home /
Detect a tap versus standing touch on iPhone?
So far I've tried to run a for loop while detecting a touch, and call it a tap if under X loops and a touch for over. However, that did not work nearly as well as expected. Is there a built in way to detect a tap versus a sliding touch?
Answer by Bampf · Aug 31, 2010 at 02:45 AM
In my app I assume it's a tap unless the phase becomes Moved. So when the phase is Ended, if it never became Moved, then it was a tap. This works well for (say) a scrolling list that they can either drag, or tap on one of the lines.
You could add a time limit to the above logic by adding up all the deltaTime values after the Began phase. Once the time limit is exceeded you'd treat it the same as if they moved their finger.
Remember to reset your timer and other variables when the phases Ended or Cancelled occur.
An alternative I haven't tried is using iPhoneTouch.tapCount. By reading it you might be able to detect single-taps (when the count goes to 1, get the position of the tap.) The documentation is vague on the behavior but it sounds like it has some logic for deciding when the user tapped one or more times.
Answer by jtbentley · Aug 30, 2010 at 11:54 PM
A touch has several phases (TouchPhase)...
Began, Moved, Stationary, Ended, Canceled.
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) doStuff();
Answer by geo · Jan 18, 2011 at 02:12 PM
Unity does most of the work for you here, you need to look at the tapCount of the "Ended" touch (touch.phase = TouchPhase.Ended).
In my testing, I found that if the user pressed and released on the same spot (in under ~1 second) then the tapCount of the "Ended" touch is 1. If they have dragged at all in between pressing and releasing, the tapCount is always 0. Also if they press but hold on the spot for over a second and then release, the tapCount is also 0.
If the maximum tap duration of around 1 second is too long, you'd probably need write your own version of this functionality.
Your answer
Follow this Question
Related Questions
Touch joystick tutorials ? 0 Answers
Best way to get to get touch inputs? 1 Answer
How to fix this problem? ios touch screen? 1 Answer
touch game objects on ios 1 Answer
Way to detect and get last/latest TOUCH 5 Answers