UITapGestureRecognizer - make it work on touch down, not touch up?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UITapGestureRecognizer is designed to recognize a completed tap, which means it fires after the finger comes up, not when it first touches the screen. If you need immediate touch-down behavior, the real solution is usually to use a different API rather than trying to force a tap recognizer to behave like a press recognizer.
Why UITapGestureRecognizer Fires on Touch Up
A tap is not confirmed until UIKit knows the user actually performed a tap instead of starting a drag or long press. That decision requires the framework to observe the touch sequence through the release event.
For that reason, there is no supported setting that turns UITapGestureRecognizer into a touch-down recognizer.
Use UIControl Events for Buttons
If the view is a UIButton or another UIControl, the cleanest solution is to use .touchDown.
This is the best option for controls because it matches UIKit's intended event model.
Use UILongPressGestureRecognizer with Zero Duration
For arbitrary views, a common workaround is UILongPressGestureRecognizer with minimumPressDuration = 0. It begins as soon as the finger touches down.
The important detail is checking for .began. A long-press recognizer continues to change state as the finger moves or lifts, so you do not want to trigger the action on every state update.
Use a Custom Recognizer Only When Necessary
If you need highly specific gesture semantics, you can subclass UIGestureRecognizer. That is usually overkill unless you are building a custom interaction that does not match UIKit's standard control or gesture APIs.
For many cases, the zero-duration long press is enough and is much easier to maintain.
Common Pitfalls
The biggest mistake is trying to keep UITapGestureRecognizer and expecting a property to switch it to touch down. That option does not exist because a tap is defined by a completed touch sequence.
Another issue is using UILongPressGestureRecognizer but forgetting to restrict the action to .began. If you respond on every state change, your code may fire multiple times for one finger press.
Gesture conflicts are also common. A press recognizer attached to a view inside a scroll view may compete with scrolling. In those cases, you may need delegate methods or a different interaction design.
Finally, if the target is really a button, do not bypass UIControl just to use a gesture recognizer. The built-in control events are simpler and map better to accessibility and UIKit conventions.
Summary
- '
UITapGestureRecognizerrecognizes taps on touch up, not touch down.' - Use
.touchDownonUIControlsubclasses such asUIButton. - For arbitrary views,
UILongPressGestureRecognizerwithminimumPressDuration = 0is the usual touch-down substitute. - Trigger the long-press handler only when the recognizer state becomes
.began. - Reach for a custom gesture recognizer only when standard UIKit tools do not fit the interaction.
Related reading
- UITapGestureRecognizer - single tap and double tap
- UITapGestureRecognizer breaks UITableView didSelectRowAtIndexPath
- UITapGestureRecognizer tap on self.view but ignore subviews
- UITextField - capture return button event
- UITextField auto-capitalization type - iPhone App
- UITextField border color
- UITextField text change event
- UITextField text change event
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.