Setting direction for UISwipeGestureRecognizer
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UISwipeGestureRecognizer detects swipe gestures in one of four directions: left, right, up, or down. A common misconception is that a single recognizer can detect multiple directions and tell you which one occurred. In reality, each UISwipeGestureRecognizer instance responds to only one direction (or a combined bitmask, but then you cannot distinguish which direction triggered it). To detect swipes in multiple directions independently, create a separate recognizer for each direction.
Basic Setup
The direction property defaults to .right. Set it explicitly to make intent clear.
Available Directions
Detecting Multiple Directions
Create one recognizer per direction and use the same handler:
Each recognizer fires only for its assigned direction, and gesture.direction in the handler tells you which one triggered.
Why Not Use a Combined Direction?
You can technically set multiple directions on one recognizer:
The problem: gesture.direction in the handler returns the original bitmask [.left, .right], not which specific direction was detected. You cannot distinguish left from right. Use separate recognizers instead.
Configuring Touches Required
The default is 1. Two-finger swipes are often used for secondary actions or navigation.
Swipe Gestures on UITableView / UICollectionView
Use gesture.location(in:) to determine which row or cell was swiped.
Combining with Other Gestures
When using swipe alongside pan, tap, or long-press gestures, set dependencies:
Without require(toFail:), the pan gesture (which also tracks finger movement) may intercept the swipe.
SwiftUI Equivalent
SwiftUI does not have a built-in swipe gesture recognizer. Use DragGesture and check the translation direction.
Objective-C Syntax
Common Pitfalls
- Single recognizer for all directions: Setting
direction = [.left, .right, .up, .down]on one recognizer makes it fire for any swipe but you cannot tell which direction. Use one recognizer per direction. - Swipe vs Pan conflict:
UIPanGestureRecognizerandUISwipeGestureRecognizerboth track finger movement. Userequire(toFail:)to prioritize swipe detection over pan. - Not adding to the right view: Adding a swipe recognizer to a small subview limits the swipe detection area. Add it to the outermost view that should respond.
- Forgetting
@objcon the handler: The target-action pattern requires the method to be exposed to Objective-C. In Swift, mark it with@objc. - Table view built-in swipe actions:
UITableViewhas native swipe-to-delete and leading/trailing swipe actions viatrailingSwipeActionsConfigurationForRowAt. Use those instead of custom gesture recognizers for standard row actions.
Summary
- Set
directionto one of.left,.right,.up,.downper recognizer - Create separate
UISwipeGestureRecognizerinstances for each direction you want to detect - Check
gesture.directionin the handler to determine which recognizer fired - Use
require(toFail:)to resolve conflicts with pan gestures - In SwiftUI, use
DragGesturewith translation checks instead
Related reading
.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.