Wilson
Wilson

Reputation: 49

UITapGestureRecognizer in Swift language

let hideTap = UITapGestureRecognizer(target: self, action: #selector(hideKeyboardTap))
func hideKeyboardTap(recognizer: UITapGestureRecognizer){
    self.view.endEditing(true)
}

hideTap.numberOfTapsRequired = 1
self.view.isUserinteractionEnabled = true
self.view.addGestureRecognizer(hideTap)

Xcode reports "swift compiler error: Expected declaration" when I run the above code. There must be mistake in this line hideTap.numberOfTapsRequired = 1but I could not find out. Would you please help me? Thanks.

Upvotes: 1

Views: 1029

Answers (3)

ClayJ
ClayJ

Reputation: 432

Try

let hideTap = UITapGestureRecognizer(target: self, action: Selector("hideKeyboardTap:"))

Upvotes: 0

Ronald Vo
Ronald Vo

Reputation: 64

you can try same this :

override func viewDidLoad() {
        super.viewDidLoad()       
        let hideTap = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboardTap(_:))
       hideTap.numberOfTapsRequired = 1
       self.view.isUserinteractionEnabled = true
       self.view.addGestureRecognizer(hideTap)
    }

func hideKeyboardTap(recognizer: UITapGestureRecognizer){
    self.view.endEditing(true)
}

Upvotes: 5

Mitesh jadav
Mitesh jadav

Reputation: 930

Write like this :

override func viewDidLoad() {
super.viewDidLoad()

let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
view.addGestureRecognizer(tap)
}

func doubleTapped() {
// do something cool here
}

Upvotes: 2

Related Questions