Directions use Pan Gesuture

I’m attempting to experiment with using Pan (Swift), but it keeps priortizing horizontal over vertical, all it takes is 1 pixel difference. I’ve also tried Swipe which is somewhat better, I also need to detect when guesture is done (touchEnded)

The idea is to send a “key” value depending on what is done

“U”,“D”,“L”,“R” for the major direction detected
and “#” when ended.

This way the app will continue to process the “direction” until is recieves the “#” (stop) command

Seems using TouchesXXXX works much better

   var initialPoint: CGPoint?

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            initialPoint = touch.location(in: self.view)
        }
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        keyCommand("#")
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first, let start = initialPoint else { return }
        let current     = touch.location(in: self.view)
        let dx          = current.x - start.x
        let dy          = current.y - start.y
        // Threshold to avoid noise
        let threshold: CGFloat = 20
        if abs(dx) > abs(dy) && abs(dx) > threshold {
            if dx > 0 { keyCommand("W") } else { keyCommand("E") }
            initialPoint = current
        } else if abs(dy) > threshold {
            if dy > 0 { keyCommand("S") } else { keyCommand("N") }
            initialPoint = current
        }
    }

    func keyCommand(_ key:String) {
        print("Command : \(key)") // this will execute required action
    }