Gesture Recognition
Swipe Detection
Touchscreen Interaction
Mobile Development
User Interface Design

How to recognize swipe in all 4 directions

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Recognizing swipes in all four directions is mostly a matter of comparing where a touch started with where it ended. The core logic is simple, but a usable implementation also needs thresholds so tiny finger jitter is not mistaken for a gesture. Once you combine distance and direction, detecting left, right, up, and down becomes straightforward.

The Core Idea

A swipe is defined by two points:

  • start position
  • end position

From those points, compute:

  • 'dx = endX - startX'
  • 'dy = endY - startY'

Then decide whether the movement was more horizontal or more vertical. If abs(dx) > abs(dy), treat it as a horizontal swipe. Otherwise, treat it as vertical.

A threshold is important because a finger almost never stays perfectly still.

A Simple JavaScript Touch Example

javascript
1let startX = 0;
2let startY = 0;
3const threshold = 30;
4
5const area = document.getElementById('swipe-area');
6
7area.addEventListener('touchstart', (event) => {
8  const touch = event.changedTouches[0];
9  startX = touch.clientX;
10  startY = touch.clientY;
11});
12
13area.addEventListener('touchend', (event) => {
14  const touch = event.changedTouches[0];
15  const dx = touch.clientX - startX;
16  const dy = touch.clientY - startY;
17
18  if (Math.abs(dx) < threshold && Math.abs(dy) < threshold) {
19    return;
20  }
21
22  if (Math.abs(dx) > Math.abs(dy)) {
23    if (dx > 0) {
24      console.log('swipe right');
25    } else {
26      console.log('swipe left');
27    }
28  } else {
29    if (dy > 0) {
30      console.log('swipe down');
31    } else {
32      console.log('swipe up');
33    }
34  }
35});

This handles the four main directions with a minimum movement requirement.

Why Thresholds Matter

Without a threshold, almost every tap becomes a tiny swipe because touch coordinates are never perfectly identical between start and end.

A good threshold depends on:

  • screen density
  • expected gesture size
  • whether the UI is scroll-heavy or tap-heavy

Small thresholds feel sensitive but noisy. Large thresholds reduce false positives but can make the UI feel unresponsive.

Handling Speed And Intent

Sometimes distance is not enough. A slow drag may not be intended as a swipe. In that case, add timing.

javascript
1let startTime = 0;
2
3area.addEventListener('touchstart', (event) => {
4  const touch = event.changedTouches[0];
5  startX = touch.clientX;
6  startY = touch.clientY;
7  startTime = Date.now();
8});
9
10area.addEventListener('touchend', (event) => {
11  const elapsed = Date.now() - startTime;
12  if (elapsed > 500) {
13    return;
14  }
15  // direction logic continues here
16});

This helps separate deliberate swipes from long drags or accidental movement.

Keep Gesture Detection Separate From UI Action

A good design pattern is to separate gesture detection from what the app does afterward. First determine the direction, then call a callback or handler.

javascript
1function handleSwipe(direction) {
2  if (direction === 'left') {
3    console.log('move to next card');
4  }
5}

That keeps the detection code reusable and makes testing easier.

Common Pitfalls

The most common mistake is ignoring thresholds, which turns taps and small movements into false swipe events.

Another issue is deciding direction from raw dx or dy alone without comparing their magnitudes. A diagonal gesture should usually resolve to the dominant axis, not trigger both directions.

It is also easy to forget multi-touch behavior. If the interface supports pinch or rotate gestures, single-finger swipe logic should ignore multi-touch interactions.

Finally, be careful not to fight native scrolling. In many interfaces, vertical swipes belong to scroll behavior unless your app has a very deliberate reason to override them.

Summary

  • Detect swipe direction by comparing touch start and touch end positions.
  • Use dx and dy, then pick the dominant axis.
  • Add a movement threshold so taps are not misclassified as swipes.
  • Consider time thresholds if you want to distinguish swipes from slow drags.
  • Keep gesture detection separate from the UI action triggered by the gesture.

Course illustration
Course illustration

All Rights Reserved.