Swipe gesture in Swift 3

Step 1: Add swipe Gesture(s) in viewDidLoad() method.

override func viewDidLoad() {
    super.viewDidLoad()
        
    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeLeft.direction = .left
    self.view.addGestureRecognizer(swipeLeft)
        
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeRight.direction = .right
    self.view.addGestureRecognizer(swipeRight)
        
    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeUp.direction = .up
    self.view.addGestureRecognizer(swipeUp)
        
    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(handleGesture))
    swipeDown.direction = .down
    self.view.addGestureRecognizer(swipeDown)
}

Step 2: Check the gesture detection in handleGesture() method

func handleGesture(gesture: UISwipeGestureRecognizer) {
   if gesture.direction == .right {
        print("Swipe Right")
   }
   else if gesture.direction == .left {
        print("Swipe Left")
   }
   else if gesture.direction == .up {
        print("Swipe Up")
   }
   else if gesture.direction == .down {
        print("Swipe Down")
   }
}

I hope this will help someone.

UPDATE:

You need to use @objc to call your 'Selector' method for latest swift versions. ie,

@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
  .
  .
  .
}

Swift 4, Xcode 9.2 This code will enable you to Recognize the Swipe Direction of the user on the whole ViewController (All of the iPhone screen, not specific to a button) Thanks @Ram-madhavan

import UIKit

class ViewController: UIViewController {

    //Label to show test and see Gesture direction. 

    @IBOutlet weak var swipeDirectionLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
      //Defining the Various Swipe directions (left, right, up, down)  
        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeLeft.direction = .left
        self.view.addGestureRecognizer(swipeLeft)

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeRight.direction = .right
        self.view.addGestureRecognizer(swipeRight)

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeUp.direction = .up
        self.view.addGestureRecognizer(swipeUp)

        let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleGesture(gesture:)))
        swipeDown.direction = .down
        self.view.addGestureRecognizer(swipeDown) 
    }

    //Function to Print to console Swipe Direction, and Change the label to show the directions. The @objc before func is a must, since we are using #selector (above). You can add to the function, in my case, I'll add a sound, so when someone flips the page, it plays a page sound. 

    @objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
        if gesture.direction == UISwipeGestureRecognizerDirection.right {
            print("Swipe Right")
            swipeDirectionLabel.text = "Swiped Right"
        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.left {
            print("Swipe Left")
            swipeDirectionLabel.text = "Swiped Left"

        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.up {
            print("Swipe Up")
            swipeDirectionLabel.text = "Swiped Up"

        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.down {
            print("Swipe Down")
            swipeDirectionLabel.text = "Swiped Down"

        }
    }
}

Build


I was having the same problem. Actually, any swipe just crashed my code (from Rob Percival, right?). So I downloaded his finished code, opened it in XCode 8 and let it convert to Swift 3. Two points were changed.

In ViewDidLoad:
  let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:)))

And inside the function:

func swiped(_ gesture: UIGestureRecognizer)

In, Swift 3 you can try this.

let swipeRightOrange = UISwipeGestureRecognizer(target: self, action:#selector(slideToRightWithGestureRecognizer))
swipeRightOrange.direction = UISwipeGestureRecognizerDirection.Right;

let swipeLeftOrange:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(slideToLeftWithGestureRecognizer))
swipeLeftOrange.direction = UISwipeGestureRecognizerDirection.Left;

@IBAction func  slideToLeftWithGestureRecognizer(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.blueColor()
}
@IBAction func slideToRightWithGestureRecognizer
(gestureRecognizer:UISwipeGestureRecognizer)
{
viewOrange.backgroundColor = UIColor.lightGrayColor()
}

var swipeGesture = UISwipeGestureRecognizer()

Take view and set IBOutlet:

@IBOutlet weak var viewSwipe: UIView!

Write this pretty code on viewDidLoad()

let direction: [UISwipeGestureRecognizerDirection] = [.up, .down, .left, .right]
    for dir in direction{
        swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeView(_:)))
        viewSwipe.addGestureRecognizer(swipeGesture)
        swipeGesture.direction = dir
        viewSwipe.isUserInteractionEnabled = true
        viewSwipe.isMultipleTouchEnabled = true
    }

Now, this is method is calling when swipe gesture is recognized.

@objc func swipeView(_ sender:UISwipeGestureRecognizer){
    UIView.animate(withDuration: 1.0) {
        if sender.direction == .right{
            self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: self.viewSwipe.frame.origin.y, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }else if sender.direction == .left{
            self.viewSwipe.frame = CGRect(x: 0, y: self.viewSwipe.frame.origin.y, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }else if sender.direction == .up{
             self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: 0, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }else if sender.direction == .down{
            self.viewSwipe.frame = CGRect(x: self.view.frame.size.width - self.viewSwipe.frame.size.width, y: self.view.frame.size.height - self.viewSwipe.frame.size.height, width: self.viewSwipe.frame.size.width, height: self.viewSwipe.frame.size.height)
        }
    }
}

100% working in my project and tested