johnbakers
johnbakers

Reputation: 24771

Using UITapGestureRecognizer rather than manually calling tapCount

I've been checking for multiple taps, whether it is 2 or 10 by simply calling tapCount on any touch:

[[touches anyObject] tapCount]==2

This simply checks for a double tap.

It works fine. I'm wondering if there is any particular reason to instead start using UITapGestureRecognizer.

It would seem that the UITapGestureRecognizer API provides wrappers around the same functionality as just inspecting touches directly, as above. Things like tapCount and the number of fingers on the screen don't require UITapGestureRecognizer.

For things like swipes, I can see the simplicity in letting UIKit handle recognizing those, as they are harder to code manually, but for a tapCount? Where's the real gain here, what am I missing?

Upvotes: 0

Views: 233

Answers (2)

Steven McGrath
Steven McGrath

Reputation: 1727

Gesture recognizers provide for coordination in processing multiple gesture types on the same view. See the discussion of the state machine in the documentation.

If a tap is the only gesture of interest, you may not find much value, but the architecture comes in handy if you want to coordinate the recognition of taps with other gestures provided either by you or system supplied classes, such as scroll views. Gesture recognizers are given first crack at touches, so you will need to use this architecture, if you want, for example, to recognize touches in a child of a scroll view, before the scroll view processes them.

The gesture recognizers can also be set to defer recognition, so, for example, the action for a single tap is not called until a double tap has timed out.

In general, the gesture recognizer approach is a good one to adopt because it allows gestures to be managed in a consistent fashion across apps and code sources. If Apple wanted to add an assistive technology preference that allowed the user to select a longer over which a double tap would be recognized. they could do this without requiring any changes to the code of developers using standard gesture recognizers.

I should add that gesture recognizers can be added directly to your storyboard or nib, so in most cases you only need to code the target action, which could be a time saver in new code.

Upvotes: 1

Mike Katz
Mike Katz

Reputation: 2080

UITapGestureRecognizer provides a cleaner, easier to use API, but no new functionality. So for your case, no reason.

Upvotes: 0

Related Questions