In the ever-evolving world of mobile app development, user experience reigns supreme. As smartphone users become increasingly sophisticated, developers are constantly seeking innovative ways to make their apps more intuitive and user-friendly. One such method that has gained widespread popularity is the implementation of swipe gestures.

Swipe gestures have become an integral part of the iOS user interface, allowing users to effortlessly navigate, interact, and access various features within apps. Whether you want to create a Tinder-like card-swiping interface, enable users to swipe to delete items, or simply enhance the overall user experience, mastering swipe gestures is essential for any iOS developer.

In this comprehensive tutorial, we will delve deep into the world of these gestures on iOS. From the basics of recognizing and handling swipes to advanced techniques like custom animations and handling multiple gestures simultaneously, this guide will equip you with the knowledge and skills needed to take your iOS app’s user experience to the next level. So, roll up your sleeves and get ready to swipe your way to a more engaging and user-friendly app!

Detecting Left and Right Swipe Gestures in iOS

Apple’s iOS SDK offers extensive tools for developers, one of which is the ability to detect and respond to a plethora of user gestures. In this comprehensive guide, the focus is to break down the process of detecting left and right swipe gestures, and then leveraging this capability to move a label in response to the swipe direction. This tutorial leverages the functionalities available in Xcode 10 and is optimized for devices running iOS 12. Learn how to implement a search bar in SwiftUI.

Initial Project Setup:

  1. Launch Xcode: Start by opening Xcode and selecting the option to create a new Single View App;
  2. Naming the Project: Name your project as ‘IOSSwipeGestureTutorial’. After that, proceed by entering the Organization Name and Organization Identifier based on your usual conventions. Make sure to select ‘Swift’ for the programming language and then click on the ‘Next’ button.

Designing the Interface:

  1. Move to the Storyboard;
  2. Drag and place a Label onto the main view. This label will be the object we’ll move later with our swipe gestures;
  3. Rename this Label to “Swipe” to give it an appropriate identifier;
  4. To ensure the label displays correctly on different screen sizes and orientations, address any layout issues by selecting the ‘Resolve Auto Layout Issues’ option and choosing ‘Reset to Suggested Constraints’.

Setting up Connections:

  1. Activate the Assistant Editor. This dual-view mode in Xcode lets you see both your interface and code side by side;
  2. Make sure the ViewController.swift file is in view on the code side;
  3. Establish a connection between the label on the Storyboard and your code by holding down the Ctrl key, dragging from the label to the ViewController class, and creating an Outlet.

Coding the Gesture Recognition:

Modify the viewDidLoad Method:

Update the method with the following code:

override func viewDidLoad() {
    let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
    let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
    leftSwipe.direction = .left
    rightSwipe.direction = .right


This code initializes two Swipe Gesture Recognizers – one for the left and the other for the right direction. Once initialized, they are added to the view. When a swipe gesture is identified, the system triggers the handleSwipes(_:) method.

Implement the handleSwipes Method:

Add this method to respond to the swipe gestures:

@objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {
    if (sender.direction == .left) {
        print("Swipe Left")
        let labelPosition = CGPoint(x: swipeLabel.frame.origin.x - 50.0, y: swipeLabel.frame.origin.y)
        swipeLabel.frame = CGRect(x: labelPosition.x, y: labelPosition.y, width: swipeLabel.frame.size.width, height: swipeLabel.frame.size.height)
    if (sender.direction == .right) {
        print("Swipe Right")
        let labelPosition = CGPoint(x: swipeLabel.frame.origin.x + 50.0, y: swipeLabel.frame.origin.y)
        swipeLabel.frame = CGRect(x: labelPosition.x, y: labelPosition.y, width: swipeLabel.frame.size.width, height: swipeLabel.frame.size.height)

This method detects the direction of the swipe. If a left swipe is identified, the label is shifted left by 50 points. Conversely, a right swipe shifts the label 50 points to the right.

Testing the Implementation:

Build and Run: With all the pieces in place, compile and run the application. Interact with it by swiping left or right on the label. You should witness the label moving in the direction of your swipe, confirming the success of the implementation.

By following this guide, developers gain a foundational understanding of how to detect and act upon swipe gestures in iOS applications. Such knowledge is instrumental when creating intuitive interfaces that respond fluidly to user interactions.


In wrapping up this tutorial, it’s evident that the ability to detect and respond to swipe gestures in iOS applications is an indispensable skill for creating dynamic and user-friendly interfaces. Through the step-by-step guide provided, developers are equipped with the practical know-how to implement left and right swipe gesture recognition using the iOS SDK and Xcode 10, tailored for applications running on iOS 12.

Leave a Reply