elito25
elito25

Reputation: 642

Function is not being called using UISwipeGestureRecognizer

Exactly what the title implies. How do the gesture recognizers work, specifically UIGestureRecognizer. Here is a small snippet of my code

var keyboardDismiser: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer:") keyboardDismiser.direction = .Right | .Left noteView.addGestureRecognizer(keyboardDismiser)

and

    func gestureRecognizer(sender: UISwipeGestureRecognizer!) {
    println("swipe")
    self.view.endEditing()
}

My goal is to dismiss the keyboard when switching from view to view in a UIScrollView with 3 pages. What am I doing wrong? There isn't much documentation on this in Swift.

Upvotes: 0

Views: 1376

Answers (2)

Alex
Alex

Reputation: 5119

I believe that selectors in Swift do not need the : at the end; they're just a string with the name of the function: gestureRecognizer. So this is what you should have:

var keyboardDismiser = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer")

Relevant question here.

Upvotes: 1

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

First you setting the recognizer on the note view. It will only be active on the note view.

In addition, you are not setting direction correctly. You are setting then changing the it's value. To set it to both right and left, you use the | operator. Also direction knows it a UISwipeGestureRecognizerDirection so you don't need specify that.

var keyboardDismiser = UISwipeGestureRecognizer(target: self, action: "gestureRecognizer:")
keyboardDismiser.direction = .Right | .Left
self.view.addGestureRecognizer(keyboardDismiser)

Finally, I would use endEditing() instead of resignFirstResponder().

func gestureRecognizer(sender: UISwipeGestureRecognizer!) {
    println("swipe")
    self.view.endEditing(true)
}

Hope that helps.

Upvotes: 2

Related Questions