How to recognize swipe in all 4 directions
Asked Answered
C

20

145

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..

Conditioner answered 13/6, 2014 at 23:33 Comment(0)
S
368

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
        }
    }
}
Shudder answered 14/6, 2014 at 1:37 Comment(9)
"each recognizer can only handle one direction" - thats not true. See #16185039Confection
"each recognizer can only handle one direction" - It is true in Swift and false in Objective-C to be specific.Coexist
You do not have to add UISwipeGestureRecognizerDirection in front of .Down ect. Just using swipeDown.direction = .Down if sufficient enough. Just a tip =)Wrapper
can we add same gestureRecogniser object to more than one view?I have tried but not worked.Sorilda
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.Monikamoniker
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)Unequivocal
i think you need an @objc in front of func respondToSwipeGesture for this to work in Swift 4Protist
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 selectorIntravasation
For later searcher ,in Swift 5, @objc is still needed in front of func respondToSwipeGesture method.Nervy
B
62

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)
}
Backsaw answered 3/12, 2015 at 21:44 Comment(3)
self.addGestureRecognizer(gesture) caused an error for me. What fixed it was self.view.addGestureRecognizer(gesture);.Norvan
@Norvan I was using my code in a UIView subclass, but you're absolutely right if you're in a UIViewController!Backsaw
Clean, best answer for me.Papoose
V
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")
}  
Valuate answered 18/11, 2015 at 17:5 Comment(2)
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.Valuate
I used 4 swipe gestures instead of one and lots of the work load is in the storyboard instead of coding.Valuate
L
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)
Lyell answered 20/1, 2016 at 14:2 Comment(2)
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.Mcbee
No longer works. In Swift 3, the context must be supplied to the first value: [UISwipeGestureRecognizerDirection.right, .left, .up, .down]Handpick
L
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")
    }
}
Landgrave answered 30/3, 2017 at 0:33 Comment(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 XcodeStaging
D
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.

Damick answered 4/6, 2018 at 8:48 Comment(0)
L
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")
    }
}
Lampley answered 3/11, 2019 at 22:27 Comment(0)
S
5

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
    }
}
Shortridge answered 8/3, 2022 at 14:59 Comment(0)
L
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
Ladner answered 11/11, 2016 at 16:26 Comment(1)
the same problem still in xcode 8.3 - compiled but not working - I got just for left and right this back UISwipeGestureRecognizerDirection(rawValue: 15)Landgrave
H
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")
    }
}
Hilten answered 26/7, 2019 at 8:34 Comment(0)
M
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.    
}
Mailman answered 6/9, 2018 at 21:1 Comment(0)
B
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
}
Bertelli answered 15/9, 2019 at 12:20 Comment(0)
P
3

In Swift 5,

let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe))
swipeGesture.direction = [.left, .right, .up, .down]
view.addGestureRecognizer(swipeGesture)
Phelps answered 12/11, 2019 at 8:0 Comment(0)
P
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"
                
            }
        }
    }

}
Pendley answered 10/11, 2021 at 9:28 Comment(1)
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 How to Answer.Unbidden
H
3

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)
    }
}
Happenstance answered 26/10, 2022 at 12:0 Comment(2)
Great answer and general advice.Waybill
Outstandingly good jobHeidyheifer
W
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))
Waybill answered 21/12, 2022 at 9:42 Comment(1)
I highly approve of this, and we do this too!Happenstance
G
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)
 })
Gelation answered 7/9, 2017 at 20:59 Comment(0)
P
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
        }
    }
}
Piroshki answered 19/9, 2017 at 6:15 Comment(0)
C
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)

    }
Chutzpah answered 10/8, 2019 at 19:35 Comment(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.Peonage
M
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
        }
    }
}
Mary answered 28/7, 2017 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.