-1
@objc func handleSwipe(gesture: UIGestureRecognizer) {
    if let gesture = gesture as? UISwipeGestureRecognizer {
        switch gesture.direction {
        case .up:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 110))
            print("Swiped up")
        case .down:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: -110))
            print("Swiped down")
        case .right:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 110, dy: 0))
            print("Swiped right")
        case .left:
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 0))
            ballPlayer.physicsBody?.applyImpulse(CGVector(dx: -110, dy: 0))
            print("Swiped left")
        default:
            print("No such gesture")
        }
    }

}

I am trying to make my sprite node move in ALL directions, including diagonally and every angle between 90 degrees and 45 degrees. This is what I have and can't find out what to do now. Any help?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Monchao
  • 15
  • 10

1 Answers1

0

I don't think UISwipeGestureRecognizer is flexible enough for your needs. You might need to get the vector from the start of the touch event to the end using a UIPanGestureRecognizer

This tutorial on UIGestureRecognizer should have what you're looking for, specifically the section on deceleration.

  • It does seem like I can use that wit SpriteKit, which is what I'm using for my app. – Monchao Dec 04 '18 at 14:27
  • It should be possible to use with SpriteKit. [Here is a tutorial on UIPanGestureRecognizer with SpriteKit](https://www.raywenderlich.com/1748-sprite-kit-tutorial-drag-and-drop-sprites). See the section on how to use gesture recognizers near the end. – guard working else panic Dec 05 '18 at 17:14