UITapGestureRecognizer - single tap and double tap
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Supporting both single and double tap on the same view is a classic iOS gesture coordination problem. If recognizers are configured independently, a double tap can incorrectly trigger single-tap logic first. The correct setup uses explicit failure dependency so the single tap waits for double-tap recognition to fail.
Configure Recognizers With Clear Responsibilities
Create two recognizers, assign different tap counts, and attach both to the same target view.
require(toFail:) is the critical line. Without it, tap timing can produce ambiguous behavior.
Keep Handlers Lightweight
Gesture callbacks run on the main thread. Heavy work inside handlers causes stutter and delayed feedback.
Good pattern:
- Keep UI feedback immediate.
- Dispatch heavy work to background tasks.
- Avoid network requests directly from gesture handlers.
This keeps interaction responsive even on older devices.
Manage Gesture Conflicts With Scroll Views
Taps often coexist with panning and zooming. For example, inside a UIScrollView, double tap might zoom while single tap toggles chrome controls. Use gesture delegate methods when default conflict resolution is insufficient.
Most screens should keep this logic simple. Complex simultaneous recognition policies are harder to debug.
Accessibility and UX Considerations
Double tap precision can be difficult for some users. If double tap triggers a critical action, provide a visible alternative control such as a button or menu item. Also verify behavior with larger text and VoiceOver enabled, since interaction rhythm can change.
For discoverability, consider a one-time hint such as “Double tap to zoom.” Hidden gestures reduce usability when not obvious.
Testing Strategy
Automated UI tests for tap timing can be fragile if assertions depend on animation timing. Keep tests robust by validating state changes instead of animation frame moments.
Manual testing checklist:
- Fast double tap.
- Slow double tap.
- Single tap after double tap.
- Interaction inside scrollable container.
This catches most coordination regressions.
Common Pitfalls
A common pitfall is adding both recognizers without setting failure dependency, which causes duplicate or incorrect actions. Another issue is handling business logic directly in gesture callbacks, leading to frame drops. Teams also forget to enable user interaction on image views, so recognizers never fire. Gesture conflicts with scroll views are another frequent source of inconsistent behavior. Finally, accessibility is often treated late, resulting in gesture-only controls that some users cannot reliably trigger.
Summary
- Use separate recognizers for single and double tap on the same view.
- Make single tap wait for double tap failure with
require(toFail:). - Keep handlers fast and move heavy work out of UI callbacks.
- Define gesture conflict policy explicitly when scroll and zoom gestures exist.
- Provide accessible alternatives for critical actions not everyone can perform via double tap.
Related reading
- 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
- UITextField value changed not firing when field is updated
.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.