5

I am trying to detect the swipes on the AppleWatch on all four directions. Yet I am not clear how to assign many values to the direction of the WKSwipeGestureRecognizer I inserted in the Storyboard. I tried with:

swiper.direction = [.right , .left ,  .up , .down]

that was funnily accepted by the compiler, differently of using the bitwise |, but, using this configuration, function:

@IBAction func swipe(_ sender: Any) {
    switch swiper.direction {
    case WKSwipeGestureRecognizerDirection.right:
        print("Swiped right")
    case WKSwipeGestureRecognizerDirection.down:
        print("Swiped down")
    case WKSwipeGestureRecognizerDirection.left:
        print("Swiped left")
    case WKSwipeGestureRecognizerDirection.up:
        print("Swiped up")
    default:
        break
    }

}

is very seldom called and when it is the swiper.direction is invariably .right.

Apple is very cryptic at: https://developer.apple.com/reference/watchkit/wkswipegesturerecognizer/1650828-direction by saying:

"The default value of this property is right. You may specify more than one direction to track swipes in multiple directions with the same gesture recognizer."

without revealing how.

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
  • Possible duplicate of [How to recognize swipe in all 4 directions](http://stackoverflow.com/questions/24215117/how-to-recognize-swipe-in-all-4-directions) – jscs Oct 13 '16 at 19:40
  • This is not well-explained by the documentation, but the value of the recognizer's `direction` in the handler is _not_ the actual direction of the detected swipe. It's simply the value that you originally set. – jscs Oct 13 '16 at 19:41
  • You should check the OptionSet docs. They very clearly explain how to define multiple options. – jjatie Oct 13 '16 at 19:41
  • The usage of the option set is correct here, @jjatie. – jscs Oct 13 '16 at 19:44
  • @JoshCaswell yes, but the OP appears uncertain as to why. – jjatie Oct 13 '16 at 19:45
  • Differently of the quoted post, I would like to use the gesture recogniser in the storyboard rather then defining it programmatically, as Apple suggests. As for the optionset docs, they indicate to use the array notation as I did. As for the middle poster, if that is not the actual direction, what it is? And more importantly, how do I get the actual swipe direction? – Fabrizio Bartolomucci Oct 13 '16 at 19:45

2 Answers2

0

At any rate, totally ignoring what Apple says, putting four WKSwipeGestureRecognizer in the storyboard each with a different direction and target does the job.

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
0

1) Select WKSwipeGestureRecognizer from Utilities box on the right side

2) From the attributes inspectors, check "Delays touches began" and select "Swipe" type that you need

 -

3) Ctrl + drag IBAction to your viewController and implement function

Developer Guy
  • 2,318
  • 6
  • 19
  • 37