UISwipeGestureRecognizer
iOS development
gesture recognition
user interface design
Swift programming

Setting direction for UISwipeGestureRecognizer

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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

swift
1class ViewController: UIViewController {
2    override func viewDidLoad() {
3        super.viewDidLoad()
4
5        let swipeRight = UISwipeGestureRecognizer(
6            target: self,
7            action: #selector(handleSwipe(_:))
8        )
9        swipeRight.direction = .right
10        view.addGestureRecognizer(swipeRight)
11    }
12
13    @objc func handleSwipe(_ gesture: UISwipeGestureRecognizer) {
14        print("Swiped right!")
15    }
16}

The direction property defaults to .right. Set it explicitly to make intent clear.

Available Directions

swift
1UISwipeGestureRecognizer.Direction.right  // →
2UISwipeGestureRecognizer.Direction.left   // ←
3UISwipeGestureRecognizer.Direction.up     // ↑
4UISwipeGestureRecognizer.Direction.down   // ↓

Detecting Multiple Directions

Create one recognizer per direction and use the same handler:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3
4    let directions: [UISwipeGestureRecognizer.Direction] = [
5        .right, .left, .up, .down
6    ]
7
8    for direction in directions {
9        let swipe = UISwipeGestureRecognizer(
10            target: self,
11            action: #selector(handleSwipe(_:))
12        )
13        swipe.direction = direction
14        view.addGestureRecognizer(swipe)
15    }
16}
17
18@objc func handleSwipe(_ gesture: UISwipeGestureRecognizer) {
19    switch gesture.direction {
20    case .right: print("Swiped right →")
21    case .left:  print("Swiped left ←")
22    case .up:    print("Swiped up ↑")
23    case .down:  print("Swiped down ↓")
24    default:     break
25    }
26}

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:

swift
1let swipe = UISwipeGestureRecognizer(
2    target: self,
3    action: #selector(handleSwipe(_:))
4)
5swipe.direction = [.left, .right]  // Recognizes both
6view.addGestureRecognizer(swipe)

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

swift
1let swipe = UISwipeGestureRecognizer(
2    target: self,
3    action: #selector(handleSwipe(_:))
4)
5swipe.direction = .right
6swipe.numberOfTouchesRequired = 2  // Requires a two-finger swipe
7view.addGestureRecognizer(swipe)

The default is 1. Two-finger swipes are often used for secondary actions or navigation.

Swipe Gestures on UITableView / UICollectionView

swift
1class TableViewController: UITableViewController {
2    override func viewDidLoad() {
3        super.viewDidLoad()
4
5        let swipeLeft = UISwipeGestureRecognizer(
6            target: self,
7            action: #selector(handleSwipe(_:))
8        )
9        swipeLeft.direction = .left
10        tableView.addGestureRecognizer(swipeLeft)
11    }
12
13    @objc func handleSwipe(_ gesture: UISwipeGestureRecognizer) {
14        let point = gesture.location(in: tableView)
15        if let indexPath = tableView.indexPathForRow(at: point) {
16            print("Swiped left on row \(indexPath.row)")
17        }
18    }
19}

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:

swift
1let panGesture = UIPanGestureRecognizer(
2    target: self,
3    action: #selector(handlePan(_:))
4)
5let swipeGesture = UISwipeGestureRecognizer(
6    target: self,
7    action: #selector(handleSwipe(_:))
8)
9swipeGesture.direction = .right
10
11// Require swipe to fail before pan activates
12panGesture.require(toFail: swipeGesture)
13
14view.addGestureRecognizer(panGesture)
15view.addGestureRecognizer(swipeGesture)

Without require(toFail:), the pan gesture (which also tracks finger movement) may intercept the swipe.

SwiftUI Equivalent

swift
1import SwiftUI
2
3struct SwipeView: View {
4    var body: some View {
5        Text("Swipe me")
6            .frame(maxWidth: .infinity, maxHeight: .infinity)
7            .gesture(
8                DragGesture(minimumDistance: 50)
9                    .onEnded { value in
10                        let horizontal = value.translation.width
11                        let vertical = value.translation.height
12
13                        if abs(horizontal) > abs(vertical) {
14                            if horizontal > 0 {
15                                print("Swiped right →")
16                            } else {
17                                print("Swiped left ←")
18                            }
19                        } else {
20                            if vertical > 0 {
21                                print("Swiped down ↓")
22                            } else {
23                                print("Swiped up ↑")
24                            }
25                        }
26                    }
27            )
28    }
29}

SwiftUI does not have a built-in swipe gesture recognizer. Use DragGesture and check the translation direction.

Objective-C Syntax

objc
1UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
2    initWithTarget:self
3    action:@selector(handleSwipe:)];
4swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
5[self.view addGestureRecognizer:swipeLeft];
6
7- (void)handleSwipe:(UISwipeGestureRecognizer *)gesture {
8    if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) {
9        NSLog(@"Swiped left");
10    }
11}

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: UIPanGestureRecognizer and UISwipeGestureRecognizer both track finger movement. Use require(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 @objc on 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: UITableView has native swipe-to-delete and leading/trailing swipe actions via trailingSwipeActionsConfigurationForRowAt. Use those instead of custom gesture recognizers for standard row actions.

Summary

  • Set direction to one of .left, .right, .up, .down per recognizer
  • Create separate UISwipeGestureRecognizer instances for each direction you want to detect
  • Check gesture.direction in the handler to determine which recognizer fired
  • Use require(toFail:) to resolve conflicts with pan gestures
  • In SwiftUI, use DragGesture with translation checks instead

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.