ios键盘输入时点击空白处回收键盘

今天想自己做键盘回收点击空白处IQKeyboardManager 类库觉得不好控制,所以自己写写, 找了下网上没找到, 想想就翻了下IQkeyboardManager得源码,找到了方法,所以写篇日记记录下

1
2
3
///两个消息处理
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.textFieldViewDidBeginEditing(_:)), name: UITextViewTextDidBeginEditingNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.textFieldViewDidEndEditing(_:)), name: UITextViewTextDidEndEditingNotification, object: nil)

有键盘开始的时候, 加入tap , 消失的时候取消, 重点其实是window 当时自己没想到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func winTap(tap:UIGestureRecognizer) {
self.inputeTextView.resignFirstResponder()
}
func textFieldViewDidBeginEditing(note:NSNotification) {
tap = UITapGestureRecognizer(target: self, action: #selector(winTap(_:)))
tap.cancelsTouchesInView = false
tap.enabled = true
tap.delegate = self
self.inputeTextView.window?.addGestureRecognizer(tap)// 只有显示才会有window
}
func textFieldViewDidEndEditing(note:NSNotification) {
self.inputeTextView.removeGestureRecognizer(tap)
}

注 ,其他的如textFiled 同理换个消息而已

代理函数去区分点击的是什么 是响应什么动作

1
2
3
4
5
6
7
8
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool{
if ((touch.view?.isKindOfClass(UIControl.classForCoder())) == true || (touch.view?.isKindOfClass(UINavigationBar.classForCoder())) == true ){
//oc 键盘类库([[touch view] isKindOfClass:[UIControl class]] || [[touch view] isKindOfClass:[UINavigationBar class]]) ? NO : YES;
return false
}
return true
}
Author

陈昭

Posted on

2016-09-14

Updated on

2021-12-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Kommentare

You forgot to set the shortname for Disqus. Please set it in _config.yml.