Search code examples
swiftsprite-kitswipe

UIGestureReconizer Crash - Unrecognized Gesture Error


Hi so my GestureReconizers crash my app when I use them. I get an error saying that it received an unrecognized gesture and I get a SIGABRT error too. I've tried looking at other posts and theres been no fix yet. [BTW, this is in my didMoveToView section]

var upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes;"))
var downSwipe = UISwipeGestureRecognizer(target: self, action:     Selector("handleSwipes;"))
var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes;"))
var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes;"))

    upSwipe.direction = .Up
    downSwipe.direction = .Down
    rightSwipe.direction = .Right
    leftSwipe.direction = .Left

    view.addGestureRecognizer(upSwipe)
    view.addGestureRecognizer(downSwipe)
    view.addGestureRecognizer(leftSwipe)
    view.addGestureRecognizer(rightSwipe)

    func handleSwipes(sender: UISwipeGestureRecognizer) {
        if sender.direction == .Left {
            msgText("Left")
        }
        if sender.direction == .Right {
            msgText("Right")
        }
        if sender.direction == .Up {
            msgText("Up")
        }
        if sender.direction == .Down {
            msgText("Down")
        }

       }

I'm also trying to have the gestures only active when map.zPosition == 100. So if you know a good way to do that, that'd be awesome.


Solution

  • You should use colon ":" for handle Swipes not semi colon ";"

    That was the reason your code was crashing

    Here is your fixed code

    override func viewDidLoad() {
        super.viewDidLoad()
    
        var upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
        var downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
        var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
        var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    
        leftSwipe.direction = .Left
        rightSwipe.direction = .Right
        upSwipe.direction = .Up
        downSwipe.direction = .Down
    
        view.addGestureRecognizer(leftSwipe)
        view.addGestureRecognizer(rightSwipe)
        view.addGestureRecognizer(upSwipe)
        view.addGestureRecognizer(downSwipe)
    
    }
    func handleSwipes(sender:UISwipeGestureRecognizer) {
        if (sender.direction == .Left) {
            print("Swipe Left")
    
        }
    
        if (sender.direction == .Right) {
            print("Swipe Right")
        }
        if (sender.direction == .Up) {
            print("Swipe Up")
        }
        if (sender.direction == .Down) {
            print("Swipe Down")
        }
    }
    

    Output

    ouput