未调用手势识别器

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!)
}

这篇关于未调用手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)