1

For my own convenience, I have created a single UISwipeGestureRecognizer allowing the user to swipe left and right invoking the same action in code.
I was wondering whether it is possible to detect the actual direction the user swiped inside its action. I found this post that discusses just that question already, but maybe something changed with Swift.

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
  • 2
    Probably not the answer you were looking for but nothing has changed as of June 16 '17 in terms of being able to detect all swipe directions through a single swipe recognizer from [Apple's API](https://developer.apple.com/documentation/uikit/uiswipegesturerecognizer). Just create 4 gestures in different direction then link them up to the same selector. – eshirima Jun 16 '17 at 14:43
  • Hm, such inconvenience, much painful! Ha. I'll leave the question open, in case someone comes around with some awesome solution. Thanks anyway! – LinusGeffarth Jun 16 '17 at 14:44
  • I mean, if you *__truly truly__* want to you could extend the Swipe Gesture Recognizer. UISwipeGestureRecognizer inherits from [UIGestureRecognizer](https://developer.apple.com/documentation/uikit/uigesturerecognizer) and with that you can get the location and other view properties which you could perform some calculations and try _predicting_ the direction. But you've really got to hate creating four swipe gesture recognizers to do this :) – eshirima Jun 16 '17 at 14:51
  • Yup, I thought of that too. I was just looking for something that's already been implemented because I often write something myself w/o knowing that it's already pre-built by Apple. I'll probably create a second recognizer. Thank you! – LinusGeffarth Jun 16 '17 at 14:53

1 Answers1

0

Reiterating on @LinusGeffarth this is the best solution I could come up with

import UIKit

class SwipeUTIL {
    static func addSwipe(selfVC: UIViewController) {
        let swipeLeft = UISwipeGestureRecognizer(target: selfVC, action: #selector(selfVC.didSwipe))
        let swipeRight =  UISwipeGestureRecognizer(target: selfVC, action: #selector(selfVC.didSwipe))

        swipeLeft.direction = .left
        swipeRight.direction = .right

        selfVC.view!.addGestureRecognizer(swipeRight)
        selfVC.view!.addGestureRecognizer(swipeLeft)
    }

}

extension UIViewController {
    internal func didSwipe(swipe: UISwipeGestureRecognizer, completion: (@escaping(UISwipeGestureRecognizerDirection) -> Void)) {
        completion(swipe.direction)
    }
}

In your View Controller

 override func viewDidLoad() {
        super.viewDidLoad()
        SwipeUTIL.addSwipe(selfVC: self)
    }

    override func didSwipe(swipe: UISwipeGestureRecognizer, completion: @escaping ((_ swipedir: UISwipeGestureRecognizerDirection) -> Void)) {
        print(swipe.direction)
    }
Fallah
  • 125
  • 7
  • 1) I've created it in code, and it makes no difference whether you set it up using the storyboard, or via code. 2) `.direction` only returns the *allowed* direction(s) the user can swipe, not the direction swiped. – LinusGeffarth Jun 16 '17 at 14:08
  • As stated in my question, I am looking for a way to detect the direction swiped using a single swipe gesture recognizer, rather than creating 4 separate recognizers. – LinusGeffarth Jun 16 '17 at 14:39
  • 1
    @Fallah the OP already knows of this answer because its included in the link he's already looked into. – eshirima Jun 16 '17 at 14:46