143

I need to use swipe to recognize swipe gesture down and then right. But on swift UISwipeGestureRecognizer has predeterminate Right direction.. And I don't know how make this for use other directions..

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
user3739367
  • 4,161
  • 6
  • 20
  • 18

20 Answers20

365

You need to have one UISwipeGestureRecognizer for each direction. It's a little weird because the UISwipeGestureRecognizer.direction property is an options-style bit mask, but each recognizer can only handle one direction. You can send them all to the same handler if you want, and sort it out there, or send them to different handlers. Here's one implementation:

override func viewDidLoad() {
    super.viewDidLoad()

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

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

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {
        case .right:
            print("Swiped right")
        case .down:
            print("Swiped down")
        case .left:
            print("Swiped left")
        case .up:
            print("Swiped up")
        default:
            break
        }
    }
}

Swift 3:

override func viewDidLoad() {
    super.viewDidLoad()

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

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

func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.up:
            print("Swiped up")
        default:
            break
        }
    }
}
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
  • "each recognizer can only handle one direction" - thats not true. See http://stackoverflow.com/questions/16184539/couldnt-get-uiswipegesturerecognizer-direction-correctly – Sergey Skoblikov May 16 '15 at 22:55
  • 5
    "each recognizer can only handle one direction" - It is true in Swift and false in Objective-C to be specific. – King-Wizard Jun 06 '15 at 20:58
  • 13
    You do not have to add `UISwipeGestureRecognizerDirection` in front of `.Down` ect. Just using `swipeDown.direction = .Down` if sufficient enough. Just a tip =) – Paul Peelen Aug 28 '15 at 08:41
  • can we add same gestureRecogniser object to more than one view?I have tried but not worked. – Max Oct 14 '15 at 11:51
  • 3
    if you do @Sergey Skoblikovs's approach in swift, (by doing `swipe.direction = [.Right,.Down,.Up,.Left]`), the recognizer will not even get called, perhaps this is an issue with swift, but as of right now does not work. – Knight0fDragon Nov 18 '15 at 17:16
  • Rather than creating multiple swipe gesture organizers that which all call the same function `swipeGesture.direction = UISwipeGestureRecognizerDirection.right self.view.addGestureRecognizer(swipeGesture) swipeGesture.direction = UISwipeGestureRecognizerDirection.left self.view.addGestureRecognizer(swipeGesture)` – Vrezh Gulyan Nov 19 '16 at 06:18
  • i think you need an `@objc` in front of `func respondToSwipeGesture` for this to work in Swift 4 – mway May 27 '19 at 03:02
  • You are correct that you need an individual swipe for each direction but you can have a gesture with more than one direction value e.g. swipe.direction = [.left, .right]. The meaning of this is that the gesture recognizer will be triggered if either of these directions occurs and it will not distinguish between them. The rawValue for the enum of UIGestureRecognizer.Direction is 1-right, 2-left, 4-up, 8-down. Setting swipe.direction = [.left, .right] will give a swipe.direction = 3 when you query the gesture in the selector – leafcutter Apr 21 '20 at 11:30
  • For later searcher ,in Swift 5, `@objc` is still needed in front of `func respondToSwipeGesture ` method. – zionpi Nov 03 '20 at 06:32
61

I just felt like contributing this, looks more elegant in the end:

func addSwipe() {
    let directions: [UISwipeGestureRecognizerDirection] = [.Right, .Left, .Up, .Down]
    for direction in directions {
        let gesture = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipe:"))
        gesture.direction = direction
        self.addGestureRecognizer(gesture)
    }
}

func handleSwipe(sender: UISwipeGestureRecognizer) {
    print(sender.direction)
}
Alexandre Cassagne
  • 2,384
  • 23
  • 40
23

From the storyboard:

  1. Add four swipe gesture recognizers to your view.
  2. Set each one with the target direction from the attribute inspector. You can select right, left, up or down
  3. One by one, select the swipe gesture recognizer, control + drag to your view controller. Insert the name (let us say leftGesture, rightGesture, upGesture and downGesture), change the connection to: Action and type to: UISwipeGestureRecognizer

From your viewController:

@IBAction func rightGesture(sender: UISwipeGestureRecognizer) {
    print("Right")
}
@IBAction func leftGesture(sender: UISwipeGestureRecognizer) {
    print("Left")
}
@IBAction func upGesture(sender: UISwipeGestureRecognizer) {
    print("Up")
}

@IBAction func downGesture(sender: UISwipeGestureRecognizer) {
    print("Down")
}  
user3099333
  • 231
  • 2
  • 3
  • 5
    By this answer you don't need to write lots of codes but you use the storyboard and connections between storyboard and your code. In case the developer prefers working in this way, this answer might be easier but if the developer prefers working more in coding, definitely the first answer is the best. – user3099333 Nov 18 '15 at 22:50
  • I used 4 swipe gestures instead of one and lots of the work load is in the storyboard instead of coding. – user3099333 Nov 20 '15 at 10:11
10

Looks like things have changed lately. In XCode 7.2 the following approach works:

override func viewDidLoad() {
    super.viewDidLoad()

    let swipeGesture = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
    swipeGesture.direction = [.Down, .Up]
    self.view.addGestureRecognizer(swipeGesture)
}

func handleSwipe(sender: UISwipeGestureRecognizer) {
    print(sender.direction)
}

Tested in Simulator on iOS 8.4 and 9.2 and on actual device on 9.2.

Or, using mlcollard's handy extension here:

let swipeGesture = UISwipeGestureRecognizer() {
    print("Gesture recognized !")
}

swipeGesture.direction = [.Down, .Up]
self.view.addGestureRecognizer(swipeGesture)
Bogdan Farca
  • 3,856
  • 26
  • 38
  • 5
    The selector may be called but the direction on the sender is incorrect. In other words, this approach is fine if you don't need to know the direction of the swipe but not otherwise. – Robert Gummesson Apr 05 '16 at 08:20
  • No longer works. In Swift 3, the context must be supplied to the first value: `[UISwipeGestureRecognizerDirection.right, .left, .up, .down]` – Brent Faust May 01 '17 at 19:54
7

Apple Swift version 3.1 - Xcode Version 8.3 (8E162)

The handy way from Alexandre Cassagne's approach

let directions: [UISwipeGestureRecognizerDirection] = [.up, .down, .right, .left]
for direction in directions {
    let gesture = UISwipeGestureRecognizer(target: self, action: #selector(YourClassName.handleSwipe(gesture:)))
    gesture.direction = direction
    self.view?.addGestureRecognizer(gesture)   
}

func handleSwipe(gesture: UISwipeGestureRecognizer) {
    print(gesture.direction)
    switch gesture.direction {
    case UISwipeGestureRecognizerDirection.down:
        print("down swipe")
    case UISwipeGestureRecognizerDirection.up:
        print("up swipe")
    case UISwipeGestureRecognizerDirection.left:
        print("left swipe")
    case UISwipeGestureRecognizerDirection.right:
        print("right swipe")
    default:
        print("other swipe")
    }
}
Community
  • 1
  • 1
Johannes Knust
  • 891
  • 1
  • 11
  • 18
  • 1
    With this specific build you need to run `defaults write com.apple.dt.xcode IDEPlaygroundDisableSimulatorAlternateFramebuffer -bool YES` in Terminal to fix a bug on some hardware. It should be fixed in a new version of Xcode – Allison Mar 30 '17 at 00:58
6

In Swift 4.2 and Xcode 9.4.1

Add Animation delegate, CAAnimationDelegate to your class

//Swipe gesture for left and right
let swipeFromRight = UISwipeGestureRecognizer(target: self, action: #selector(didSwipeLeft))
swipeFromRight.direction = UISwipeGestureRecognizerDirection.left
menuTransparentView.addGestureRecognizer(swipeFromRight)

let swipeFromLeft = UISwipeGestureRecognizer(target: self, action: #selector(didSwipeRight))
swipeFromLeft.direction = UISwipeGestureRecognizerDirection.right
menuTransparentView.addGestureRecognizer(swipeFromLeft)

//Swipe gesture selector function
@objc func didSwipeLeft(gesture: UIGestureRecognizer) {
    //We can add some animation also
    DispatchQueue.main.async(execute: {
            let animation = CATransition()
            animation.type = kCATransitionReveal
            animation.subtype = kCATransitionFromRight
            animation.duration = 0.5
            animation.delegate = self
            animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            //Add this animation to your view
            self.transparentView.layer.add(animation, forKey: nil)
            self.transparentView.removeFromSuperview()//Remove or hide your view if requirement.
        })
}

//Swipe gesture selector function
@objc func didSwipeRight(gesture: UIGestureRecognizer) {
        // Add animation here
        DispatchQueue.main.async(execute: {
            let animation = CATransition()
            animation.type = kCATransitionReveal
            animation.subtype = kCATransitionFromLeft
            animation.duration = 0.5
            animation.delegate = self
            animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            //Add this animation to your view
            self.transparentView.layer.add(animation, forKey: nil)
            self.transparentView.removeFromSuperview()//Remove or hide yourview if requirement.
        })
}

If you want to remove gesture from view use this code

self.transparentView.removeGestureRecognizer(gesture)

Ex:

func willMoveFromView(view: UIView) {
    if view.gestureRecognizers != nil {
        for gesture in view.gestureRecognizers! {
            //view.removeGestureRecognizer(gesture)//This will remove all gestures including tap etc...
            if let recognizer = gesture as? UISwipeGestureRecognizer {
                //view.removeGestureRecognizer(recognizer)//This will remove all swipe gestures
                if recognizer.direction == .left {//Especially for left swipe
                    view.removeGestureRecognizer(recognizer)
                }
            }
        }
    }
}

Call this function like

//Remove swipe gesture
self.willMoveFromView(view: self.transparentView)

Like this you can write remaining directions and please careful whether if you have scroll view or not from bottom to top and vice versa

If you have scroll view, you will get conflict for Top to bottom and view versa gestures.

Naresh
  • 16,698
  • 6
  • 112
  • 113
5

Swipe gesture to the view you want, or viewcontroller whole view in Swift 5 & XCode 11 based on @Alexandre Cassagne

override func viewDidLoad() {
    super.viewDidLoad()

    addSwipe()
}

func addSwipe() {
    let directions: [UISwipeGestureRecognizer.Direction] = [.right, .left, .up, .down]
    for direction in directions {
        let gesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe))
        gesture.direction = direction
        self.myView.addGestureRecognizer(gesture)// self.view
    }
}

@objc func handleSwipe(sender: UISwipeGestureRecognizer) {
    let direction = sender.direction
    switch direction {
        case .right:
            print("Gesture direction: Right")
        case .left:
            print("Gesture direction: Left")
        case .up:
            print("Gesture direction: Up")
        case .down:
            print("Gesture direction: Down")
        default:
            print("Unrecognized Gesture Direction")
    }
}
Egzon P.
  • 4,498
  • 3
  • 32
  • 31
4

UISwipeGestureRecognizer has a direction property that has the following definition:

var direction: UISwipeGestureRecognizerDirection

The permitted direction of the swipe for this gesture recognizer.


The problem with Swift 3.0.1 (and below) is that even if UISwipeGestureRecognizerDirection conforms to OptionSet, the following snippet will compile but won't produce any positive expected result:

// This compiles but does not work
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(gestureHandler))
gesture.direction = [.right, .left, .up, .down]
self.addGestureRecognizer(gesture)

As a workaround, you will have to create a UISwipeGestureRecognizer for each desired direction.


The following Playground code shows how to implement several UISwipeGestureRecognizer for the same UIView and the same selector using Array's map method:

import UIKit
import PlaygroundSupport

class SwipeableView: UIView {
    convenience init() {
        self.init(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        backgroundColor = .red

        [UISwipeGestureRecognizerDirection.right, .left, .up, .down].map({
            let gesture = UISwipeGestureRecognizer(target: self, action: #selector(gestureHandler))
            gesture.direction = $0
            self.addGestureRecognizer(gesture)
        })
    }

    func gestureHandler(sender: UISwipeGestureRecognizer) {
        switch sender.direction {
        case [.left]:   frame.origin.x -= 10
        case [.right]:  frame.origin.x += 10
        case [.up]:     frame.origin.y -= 10
        case [.down]:   frame.origin.y += 10
        default:        break
        }
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(SwipeableView())
    }
}

let controller = ViewController()
PlaygroundPage.current.liveView = controller
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
  • the same problem still in xcode 8.3 - compiled but not working - I got just for left and right this back `UISwipeGestureRecognizerDirection(rawValue: 15)` – Johannes Knust Mar 30 '17 at 00:08
4

Swipe Gesture in Swift 5

  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)
}

@objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
    if gesture.direction == UISwipeGestureRecognizer.Direction.right {
        print("Swipe Right")
    }
    else if gesture.direction == UISwipeGestureRecognizer.Direction.left {
        print("Swipe Left")
    }
    else if gesture.direction == UISwipeGestureRecognizer.Direction.up {
        print("Swipe Up")
    }
    else if gesture.direction == UISwipeGestureRecognizer.Direction.down {
        print("Swipe Down")
    }
}
Lijith Vipin
  • 1,870
  • 21
  • 29
3

EDİT: "swift5.3"

First create a baseViewController and add viewDidLoad this code :

class BaseViewController: UIViewController {             

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

     // Example Tabbar 5 pages
     @objc func swiped(_ gesture: UISwipeGestureRecognizer) {
         if gesture.direction == .left {
            if (self.tabBarController?.selectedIndex)! < 5 {
                self.tabBarController?.selectedIndex += 1
            }
         } else if gesture.direction == .right {
             if (self.tabBarController?.selectedIndex)! > 0 {
                 self.tabBarController?.selectedIndex -= 1
             }
         }
     }  
}

And use this baseController class:

class YourViewController: BaseViewController {
    // its done. Swipe successful
    //Now you can use all the Controller you have created without writing any code.    
}
ikbal
  • 1,110
  • 12
  • 21
3

After digging around for a while:

The shortest way to add swipes for all 4 directions is:

override func viewDidLoad() {
    super.viewDidLoad()    
    for direction in [UISwipeGestureRecognizer.Direction.down, .up, .left, .right]{
        let swipeGest = UISwipeGestureRecognizer(target: self, action: #selector(swipeAction(_:)))
        swipeGest.direction = direction
        self.view.addGestureRecognizer(swipeGest)
    }
} 

@objc func swipeAction(_ gesture: UISwipeGestureRecognizer){
    switch gesture.direction {
    case UISwipeGestureRecognizer.Direction.right:
        print("Swiped right")
    case UISwipeGestureRecognizer.Direction.down:
        print("Swiped down")
    case UISwipeGestureRecognizer.Direction.left:
        print("Swiped left")
    case UISwipeGestureRecognizer.Direction.up:
        print("Swiped up")
    default: break
}
NeonGloss
  • 51
  • 4
3

In Swift 5,

let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe))
swipeGesture.direction = [.left, .right, .up, .down]
view.addGestureRecognizer(swipeGesture)
nitin.agam
  • 1,949
  • 1
  • 17
  • 24
3
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let leftside = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
        leftside.direction = .left
        view.addGestureRecognizer(leftside)
        
        let rightside = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
        rightside.direction = .right
        view.addGestureRecognizer(rightside)
        
        
        let upside =  UISwipeGestureRecognizer(target: self, action: #selector(swiped))
        upside.direction = .up
        view.addGestureRecognizer(upside)
        
        let downside = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
        downside.direction = .down
        view.addGestureRecognizer(downside)
        
        // Do any additional setup after loading the view, typically from a nib.
    }
    @objc func swiped(gesture: UIGestureRecognizer){
        if let swipeGesture = gesture as? UISwipeGestureRecognizer{
            switch swipeGesture.direction{
            case UISwipeGestureRecognizer.Direction.left:
                view.backgroundColor = UIColor.red
            case UISwipeGestureRecognizer.Direction.right:
                view.backgroundColor = UIColor.yellow
            case UISwipeGestureRecognizer.Direction.up:
                view.backgroundColor = UIColor.green
            case UISwipeGestureRecognizer.Direction.down:
                view.backgroundColor = UIColor.blue
               
            default:
                "ERROR"
                
            }
        }
    }

}
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
mAj
  • 31
  • 2
  • 2
    Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it answers the specific question being asked. See [answer]. – ChrisGPT was on strike Nov 10 '21 at 09:49
3

Swift 5+

Add desired gestures to some UIView:

[UISwipeGestureRecognizer.Direction.up, .down, .left, .right].forEach {
    let gesture = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
    gesture.direction = $0
    someView.addGestureRecognizer(gesture)
}

Handle swipe actions:

@objc func swiped(_ gesture: UISwipeGestureRecognizer) {
    switch gesture.direction {
    case .up:       print("up")
    case .down:     print("down")
    case .left:     print("left")
    case .right:    print("right")
    default: break
    }
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
3

I like @Fattie's approach (extension), which is what I've been using for tap gestures for a long time, except as an extension of UIView, instead of UIViewController. It is a great time saving convenience. Since you may not want to enable all four cartesian direction,s I default to nil for each selector to check for presence of parameters to make activation of gesture for any given direction optional.

I made the parameter names a little bit longer to speed up reverse engineering when skimming code and overloaded the function name, because why not?

extension UIView {
    func addSwipeRecognizer(direction: UISwipeGestureRecognizer.Direction, target: Any, action: Selector) {
        let recognizer = UISwipeGestureRecognizer(target: target, action: action)
        recognizer.direction = direction
        addGestureRecognizer(recognizer)
    }
    
    func addSwipeRecognizer(target: Any, left: Selector? = nil, right: Selector? = nil, up: Selector? = nil, down: Selector? = nil) {
        if let left  { addSwipeRecognizer(direction: .left,  target: target, action: left)  }
        if let right { addSwipeRecognizer(direction: .right, target: target, action: right) }
        if let up    { addSwipeRecognizer(direction: .up,    target: target, action: up)    }
        if let down  { addSwipeRecognizer(direction: .down,  target: target, action: down)  }
    }
    
    func addTapRecognizer(tapNumber: Int, target: Any, action: Selector) {
        let tap = UITapGestureRecognizer(target: target, action: action)
        tap.numberOfTapsRequired = tapNumber
        addGestureRecognizer(tap)
        isUserInteractionEnabled = true
    }
}

Usage:

@objc func viewSwipedLeft()  { }
@objc func viewSwipedRight() { }
@objc func viewSwipedUp()    { }
@objc func viewSwipedDown()  { }

view.addSwipeRecognizer(target: self,
                             left:  #selector(viewSwipedLeft),
                             right: #selector(viewSwipedRight),
                             up:    #selector(viewSwipedUp),
                             down:  #selector(viewSwipedDown))
clearlight
  • 12,255
  • 11
  • 57
  • 75
2

For 2022

Here's how you'd probably write it in any real-life code.

You'd never clutter your VC code with all the boilerplate.

In your view controller ...

override func viewDidLoad() {
    super.viewDidLoad()
    pentaSceneView.buildFresh()
    addSwipesLRUD(#selector(swipeLeft), #selector(swipeRight),
                         #selector(swipeUp), #selector(swipeDown))
}

and ...

@objc func swipeLeft() {
    blah ...
}

@objc func swipeRight() {
    blah ...
}

@objc func swipeUp() {
    blah ...
}

@objc func swipeDown() {
    blah ...
}

Should you use a switch statement? Answer: NO, don't.

Apple's code for .direction is not so much broken, but just "incredibly stupid", and they keep changing it. A switch statement is neither more elegant nor shorter than having four functions, and it will very likely break when Apple (again) slightly change or rationalize the way .direction works.

Here's the simple extension needed.

Put this in any file in your project, say, "Utils.swift". Every real-world project has an "extensions in here" type file, where, you keep very common extensions you use in all projects.

extension UIViewController {
    ///Add a UISwipeGestureRecognizer.
    func addSwipe(_ d: UISwipeGestureRecognizer.Direction, _ s: Selector) {
        let g = UISwipeGestureRecognizer(target: self, action: s)
        g.direction = d
        view.addGestureRecognizer(g)
    }
    
    ///Add four swipes for left right up down.
    func addSwipesLRUD(_ l: Selector, _ r: Selector, _ u: Selector, _ d: Selector) {
        addSwipe(.left, l)
        addSwipe(.right, r)
        addSwipe(.up, u)
        addSwipe(.down, d)
    }
}
Fattie
  • 27,874
  • 70
  • 431
  • 719
1

Just a cooler swift syntax for Nate's answer:

[UISwipeGestureRecognizerDirection.right,
 UISwipeGestureRecognizerDirection.left,
 UISwipeGestureRecognizerDirection.up,
 UISwipeGestureRecognizerDirection.down].forEach({ direction in
    let swipe = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipe.direction = direction
    self.view.addGestureRecognizer(swipe)
 })
AmitP
  • 5,353
  • 4
  • 35
  • 27
1

Easy. Just follow the code below and enjoy.

//SwipeGestureMethodUsing
func SwipeGestureMethodUsing ()
{
    //AddSwipeGesture
    [UISwipeGestureRecognizerDirection.right,
     UISwipeGestureRecognizerDirection.left,
     UISwipeGestureRecognizerDirection.up,
     UISwipeGestureRecognizerDirection.down].forEach({ direction in
        let swipe = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
        swipe.direction = direction
        window?.addGestureRecognizer(swipe)
     })
}

//respondToSwipeGesture
func respondToSwipeGesture(gesture: UIGestureRecognizer) {

    if let swipeGesture = gesture as? UISwipeGestureRecognizer
    {
        switch swipeGesture.direction
        {
        case UISwipeGestureRecognizerDirection.right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.up:
            print("Swiped up")
        default:
            break
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Super Developer
  • 891
  • 11
  • 20
1

For Swift 5 it's updated

//Add in ViewDidLoad
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.handleSwipe))
        gesture.direction = .right
        self.view.addGestureRecognizer(gesture)

//Add New Method
@objc func handleSwipe(sender: UISwipeGestureRecognizer) {
        print("swipe direction is",sender.direction)

    }
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • 1
    How is this an answer to the OP's question? The problem was to find an elegant way to add multiple directions to the swipe gesture recognizer. – shashwat Sep 23 '19 at 05:58
0

It can be done by simply declaring one function which will handle all your swipe UISwipeGestureRecognizer directions. Here is my code:

let swipeGestureRight = UISwipeGestureRecognizer(target: self, action:#selector(ViewController.respondToSwipeGesture(_:)) )
swipeGestureRight.direction = UISwipeGestureRecognizerDirection.right
self.view .addGestureRecognizer(swipeGestureRight)

let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeGestureLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeGestureLeft)

let swipeGestureUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeGestureUp.direction = UISwipeGestureRecognizerDirection.up
self.view.addGestureRecognizer(swipeGestureUp)

let swipeGestureDown = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.respondToSwipeGesture(_:)))
swipeGestureDown.direction = UISwipeGestureRecognizerDirection.down
self.view.addGestureRecognizer(swipeGestureDown)

Here is the function which will hande the swipedirection functionality:

func respondToSwipeGesture(_ sender: UIGestureRecognizer) {
    if let swipeGesture = sender as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.right:
                print("right swipe")
            case UISwipeGestureRecognizerDirection.left:
                print("leftSwipe")
            case UISwipeGestureRecognizerDirection.up:
                print("upSwipe")
            case UISwipeGestureRecognizerDirection.down:
                print("downSwipe")
            default:
                break
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Shakti
  • 986
  • 12
  • 22