未调用手势识别器

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

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

本文标题为:未调用手势识别器

基础教程推荐