UITextView 聚焦,但用户触摸 UICollectionViewCell.隐藏键盘并运行 didSelectItemAtIndexPath [英] UITextView focused, but user touches UICollectionViewCell. Hide keyboard and run didSelectItemAtIndexPath

查看:23
本文介绍了UITextView 聚焦,但用户触摸 UICollectionViewCell.隐藏键盘并运行 didSelectItemAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图顶部有一个搜索"UITextField.在此下方,我有一个 UICollectionView,其中填充了用户键入的搜索结果.

I have a "search" UITextField at the top of my view. Below this, I have a UICollectionView which is populated with the search results as the user types.

当用户在 UITextView 中输入时,会显示键盘.起初,我想在用户触摸 UITextField 之外的任何地方时隐藏键盘.我通过以下方式完成了这项工作:

When a user is typing into the UITextView, the keyboard is displayed. At first, I wanted to hide the keyboard if the user touched anywhere outside the UITextField. I accomplished this with the following:

func textFieldDidBeginEditing(textField: UITextField) {

    if (textField == self.textFieldSearch) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFieldSearchDidChange:", name: UITextFieldTextDidChangeNotification, object: textField)
    }

    var tapGesture = UITapGestureRecognizer(target: self, action: "dismissKeyboard:")
    self.view.addGestureRecognizer(tapGesture)

}

func dismissKeyboard(gesture: UIGestureRecognizer) {
    self.textFieldSearch.resignFirstResponder()
    self.view.removeGestureRecognizer(gesture)
}

然而,如果用户点击 UICollectionViewCell,dismissKeyboard 函数会运行并隐藏键盘,但用户必须再次点击单元格才能运行函数:

However, if the user taps on a UICollectionViewCell, the dismissKeyboard func runs, and hides the keyboard, but the user has to tap on the cell again to run the func:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

我如何一步完成这一切?因此,如果用户触摸 UITextField 之外的任何位置,隐藏键盘...但如果用户碰巧触摸 UICollectionViewCell,则在第一次触摸时运行 didSelectItemAtIndexPath 函数以及隐藏键盘并结束编辑在 UITextField 上?

How do I do this all in one step? So if the user touches anywhere outside the UITextField, hide the keyboard...but if the user happens to touch a UICollectionViewCell, run the didSelectItemAtIndexPath function on the first touch as well as hide the keyboard and end editing on the UITextField?

感谢任何帮助!谢谢

推荐答案

可以实现UIGestureRecognizerDelegate

optional func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
             shouldReceiveTouch touch: UITouch) -> Bool

对于不应调用手势识别器的视图,上述方法返回 NO,在您的情况下,它应该是集合视图,否则其他事物返回 YES.

Above method returns NO for the views on which gesture recognizer should not be called and in your case it should be collection view else for other things return YES.

示例实现-

/**
    Disallow recognition of tap gestures on the collection view.
*/
    func gestureRecognizer(recognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool 
{
        if touch.view == collectionView && recognizer == tapRecognizer     {
            return false
        }
        return true
 }

这篇关于UITextView 聚焦,但用户触摸 UICollectionViewCell.隐藏键盘并运行 didSelectItemAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆