Counting touches in touchesBegan() and gestures

This discusses a surprise!  something I learned the hard way.

Many touch events may come via touchesBegan() for multi-finger touches .  For example, for a three finger touch, you may get one event having one touch followed a fraction of a second later by another event having two touches, where each touch is for a different finger (having a different location.)

In other words, you can NOT rely on getting one touch event having all the touches.    You shouldn’t check Set<UITouch>.count() to determine the number of fingers the user is using.

That is the job of a gesture recognizer.  A gesture recognizer typically establishes a short window in time, and gathers all the distinctly located touches begun in that window, to determine whether the count of touches meets the requirements of the gesture.  (I presume a gesture recognizer also filters out touches that later drop out of the gesture.)

Every gesture (the superclass  UIGestureRecognizer) has the method numberOfTouches().

Many gestures can be configured with a minimum count of touches.  This is only a minimum.  A user can use more fingers and the gesture still be recognized.

  • Pan: minimumNumberOfTouches()
  • LongPress, Tap: numberOfTouchesRequired()
  • etc.

Some of the gestures also allow a maximum to be defined.

In summary: if your app supports only one finger, you might be able to use touchesBegan() and touchesEnded() to crudely determine what the user is doing.  Otherwise, you should rely on gesture recognizers to count the number of touches.

Note that certain combinations of gestures (e.g. Tap and double Tap, with a dependency defined between them) might incur a lag for gesture recognition.   In other words, using touchesBegan, you can soon determine whether a user is acting, but using gestures you might later determine a user is acting.  It can get complicated.  For example, is the  lag between start of the real gesture and when a recognizer is in the Begin state or is the lag until the the single tap recognizer is in the Finished state (which comes no sooner than the double Tap recognizer being in the Canceled state) ?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s