Search code examples
iosswiftsprite-kituigesturerecognizerswipe-gesture

Detect coordinates on release of a swipe swift with spritekit


Hi if someone can help me with this that would be great!

I need to get coordinates of a swipe. I can get the coordinates of the initial touch from locationInNode etc, however how can we get the coordinates on the release point of a swipe?

func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    touched = true

    for touch in touches {
        location = touch.locationInNode(self)
        print(location)
    }
}

This is how i get my initial coordinates, I have also done this for touchesMoved

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        location = touch.locationInNode(self)
        print(location)
    }

with this code it does not print the final coordinate of release on touch.

Basically I would like to workout the distance between first touch and point on release of swipe with coordinates. Thanks for your help!!


Solution

  • Capture the state of the swipe, from began to ended:

    let point: CGPoint = recognizer.location(in: recognizer.view!)
    if recognizer.state == .began {
        print(NSCoder.string(for: point))
    }
    else if recognizer.state == .ended {
        print(NSCoder.string(for: point))
    }