我有一个UIView并且我添加了点击手势
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
tap.delegate = self
myView.addGesture(tap)
我试图在testfile中以编程方式调用它。
sendActionForEvent
我正在使用这个函数,但它不起作用:
myView.sendActionForEvent(UIEvents.touchUpDown)
它显示未识别的选择器发送到实例。
我该如何解决这个问题呢?
实现轻触手势
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "touchHappen")
view.userInteractionEnabled = true
view.addGestureRecognizer(tap)
当点击被识别时调用此函数。
func touchHappen() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
self.view.endEditing(true)
}
针对Swift 3 +的更新
let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchHappen(_:)))
yourView.addGestureRecognizer(tap)
yourView.userInteractionEnabled = true
func touchHappen(_ sender: UITapGestureRecognizer) {
print("Hello Dear you are here")
}