Article

Handling Swipe Gestures

Detect a horizontal or vertical swipe motion on the screen, and use it to trigger navigation through your content.

Overview

A swipe gesture occurs when the user moves one or more fingers across the screen in a specific horizontal or vertical direction. Use the UISwipeGestureRecognizer class to detect swipe gestures.

You can attach a gesture recognizer in one of these ways:

  • Programmatically. Call the addGestureRecognizer(_:) method of your view.

  • In Interface Builder. Drag the appropriate object from the library and drop it onto your view.

A diagram demonstrating a single-finger horizontal swipe gesture

A UISwipeGestureRecognizer object tracks the motion of the user’s finger across the screen either horizontally or vertically. A swipe requires the user’s finger to move in a specific direction and not deviate significantly from the main direction of travel. (The direction and number of fingers required for the gesture are configurable.) Swipe gestures are discrete, so your action method is called only after the gesture ends successfully. As a result, swipes are most appropriate when you care only about the results of the gesture and not about tracking the movement of the user’s finger.

Listing 1 shows a skeletal action method for a swipe gesture recognizer. You would use a method like this to perform a task when the gesture is recognized. Because the gesture is discrete, the gesture recognizer does not enter the began or changed states.

Listing 1

Performing a task in response to a swipe

@IBAction func swipeHandler(_ gestureRecognizer : UISwipeGestureRecognizer) {
    if gestureRecognizer.state == .ended {
        // Perform action.
    }
}

If the code for your swipe gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:

  • The isUserInteractionEnabled property of the view is set to true. Image views and labels set this property to false by default.

  • The number of touches is equal to the value specified in the numberOfTouchesRequired property.

  • The direction of the swipe matches the value in the direction property.

See Also

Gestures

Handling Tap Gestures

Use brief taps on the screen to implement button-like interactions with your content.

Handling Long-Press Gestures

Detect extended duration taps on the screen, and use them to reveal contextually relevant content.

Handling Pan Gestures

Trace the movement of fingers around the screen, and apply that movement to your content.

Handling Pinch Gestures

Track the distance between two fingers and use that information to scale or zoom your content.

Handling Rotation Gestures

Measure the relative rotation of two fingers on the screen, and use that motion to rotate your content.