UIPanGestureRecognizer - Only vertical or horizontal
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIPanGestureRecognizer reports free-form movement, but many interfaces need directional locking so interactions behave as either vertical or horizontal only. Typical examples include sliders, drawer menus, and sortable lists where diagonal movement should be ignored. A clean solution determines dominant axis early in the gesture, then constrains subsequent translation to that axis. This avoids jitter and keeps UI behavior predictable. You can implement this in gesture handling logic without custom recognizer subclasses, though subclassing can help if many views share the same rule.
Core Sections
Determine dominant axis at gesture start
Use initial translation velocity or movement delta to lock direction.
Velocity-based locking usually feels responsive in UI interactions.
Constrain translation according to locked axis
Apply only relevant component to your target transform or frame.
Reset axis at end so each new gesture can decide independently.
Use delegate rules for recognizer coordination
If panning competes with scroll views, implement recognizer delegate methods to avoid conflicts.
For nested scroll interactions, you may allow simultaneous recognition conditionally.
Add threshold to prevent accidental lock
Tiny initial movement noise can cause wrong direction lock. Add minimum displacement before locking.
This improves feel on high-sensitivity screens.
Test with real devices
Simulator input can differ from actual touch behavior. Validate on device with fast and slow swipes, one-handed interactions, and edge cases near screen boundaries.
Common Pitfalls
- Locking axis immediately on tiny jitter, resulting in erratic direction choices.
- Forgetting to reset lock state when gesture ends, causing next gesture to inherit stale axis.
- Applying both x and y deltas after deciding axis, which defeats directional constraint.
- Ignoring competition with scroll views and getting inconsistent gesture ownership.
- Validating only in simulator and missing real-device touch dynamics.
Verification Workflow
After implementing the main approach, run a short verification loop that proves behavior on realistic and adversarial inputs. Start with a small happy-path sample that should always pass, then add one edge case and one failure case that should be rejected or handled gracefully. Capture concrete outputs instead of relying on visual inspection alone. For operational code, record one measurable signal such as runtime, memory use, or error count so you can compare before and after future refactors.
Use this quick template during local development and CI:
This discipline catches most regressions caused by dependency upgrades, environment differences, or hidden assumptions in helper functions. It also makes handoffs easier because another engineer can reproduce behavior quickly without reverse-engineering your intent from source code alone.
Summary
To make UIPanGestureRecognizer vertical-only or horizontal-only, detect dominant axis early, lock it, and apply constrained translation throughout the gesture. Add a small movement threshold and reset lock state reliably at gesture completion. Coordinate with other recognizers through delegate rules for stable interaction behavior. This approach yields smooth, intentional gesture UX without heavy custom gesture infrastructure.
Related reading
- UIPopovercontroller dealloc reached while popover is still visible
- UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController
- UIRefreshControl on UICollectionView only works if the collection fills the height of the container
- UIRefreshControl without UITableViewController
- UIScrollView horizontal paging like Mobile Safari tabs
- UIScrollView not scrolling
- UIScrollView paging horizontally, scrolling vertically?
- UIScrollView pauses NSTimer until scrolling finishes
.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.