x


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?

more ▼

asked Aug 30, 2010 at 08:38 PM

karl_ gravatar image

karl_
3k 114 103 132

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

3 answers: sort newest

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.

more ▼

answered Aug 31, 2010 at 02:45 AM

Bampf gravatar image

Bampf
5.6k 13 31 66

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

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.

more ▼

answered Jan 18, 2011 at 02:12 PM

geo gravatar image

geo
17 2

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

A touch has several phases (TouchPhase)...

Began, Moved, Stationary, Ended, Canceled.

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) doStuff();

more ▼

answered Aug 30, 2010 at 11:54 PM

jtbentley gravatar image

jtbentley
624 7 7 23

(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:

x2845
x1568
x1170
x68
x27

asked: Aug 30, 2010 at 08:38 PM

Seen: 10622 times

Last Updated: Jan 18, 2011 at 02:14 PM