Gesture Recognizer Not Called(未调用手势识别器)
本文介绍了未调用手势识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经设置了一个手势识别器,用于当用户在文本字段外点击时取消键盘。DismissKeyboard
未调用函数。
我设置观察者是错误的还是这是一个不同的问题?此外,这是正在点击的表视图。
代码摘录
class CommentsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("keyboardWillShow:"),
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("keyboardWillHide:"),
name: UIKeyboardWillHideNotification,
object: nil)
}
func keyboardFrameChanged(notification: NSNotification) {
println("keyboardFrameChanged")
let userInfo = notification.userInfo
let key = UIKeyboardFrameEndUserInfoKey
if let info = userInfo {
let frameValue = info[key] as! NSValue
let _frame = frameValue.CGRectValue()
}
}
func keyboardWillShow(notification: NSNotification) {
if keyboardDismissTapGesture == nil
{
println("dismiss")
keyboardDismissTapGesture = UITapGestureRecognizer(target: self, action: Selector("dismissKeyboard:"))
self.view.addGestureRecognizer(keyboardDismissTapGesture!)
}
}
func keyboardWillHide(notification: NSNotification) {
if keyboardDismissTapGesture != nil
{
println("test2")
self.view.removeGestureRecognizer(keyboardDismissTapGesture!)
keyboardDismissTapGesture = nil
}
}
func dismissKeyboard(sender: AnyObject) {
println("dismiss keyboard")
commentTextField.resignFirstResponder()
}
我在disdissKeyboard上设置了断点,但甚至没有调用它。
输出
当我点击文本视图时,键盘打开,这是输出
keyboardFrameChanged
keyboardFrameChanged
will show
dismiss
当我点击其他任何内容(尝试关闭键盘)时,没有进一步的输出。
推荐答案
将手势识别器委派设置为您自己,并添加UIGestureRecognizerDelegate
协议。
func keyboardWillShow(notification: NSNotification) {
if keyboardDismissTapGesture == nil
{
println("dismiss")
keyboardDismissTapGesture = UITapGestureRecognizer(target: view, action: "dismissKeyboard:")
keyboardDismissTapGesture.delegate = self
self.view.addGestureRecognizer(keyboardDismissTapGesture!)
}
}
然后添加:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
我的猜测是,您的表视图的手势干扰了新的UITapGesture
。尝试此解决方案,否则在检测到新的UITapGesture
时,您的表视图操作将失败。
要使表视图的UITapGestureRecognizer
失败,我使用以下代码:
if let recognizers = yourTableView.gestureRecognizers, let index = find(recognizers.map { $0 is UITapGestureRecognizer }, true) {
(recognizers[index] as! UIPanGestureRecognizer).requireGestureRecognizerToFail(keyboardDismissTapGesture)
}
可能不是最优雅的方法,但当我想要通过UIPanGestureRecognizer
时,它对我很有效。我尚未使用UITapGestureRecognizer
进行测试。
编辑:
if let recognizers = yourTableView.gestureRecognizers, let index = find(recognizers.map { $0 is UIGestureRecognizer }, true) {
(recognizers[index] as! UIGestureRecognizer).requireGestureRecognizerToFail(keyboardDismissTapGesture!)
}
这篇关于未调用手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:未调用手势识别器
基础教程推荐
猜你喜欢
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01