Swift基础之关于键盘的那些事儿

看过小编前面的文章的小伙伴们应该已经发现了,在UITextField中,小编简单的介绍过一丢丢关于UIKeyboard的键盘类型。在这篇文章中,我们来详细的看一下UIKeyboard的相关知识吧。

一、键盘的相关属性

1.键盘8种类型
  textF.keyboardType = UIKeyboardType.Default  //系统默认的虚拟键盘
  textF.keyboardType = UIKeyboardType.ASCIICapable  //显示英文字母的虚拟键盘
  textF.keyboardType = UIKeyboardType.NumbersAndPunctuation  //显示数字和标点的虚拟键盘
  textF.keyboardType = UIKeyboardType.URL  //显示便于输入数字的虚拟键盘
  textF.keyboardType = UIKeyboardType.NumberPad  //显示便于输入数字的虚拟键盘
  textF.keyboardType = UIKeyboardType.PhonePad  //显示便于拨号呼叫的虚拟键盘
  textF.keyboardType = UIKeyboardType.NamePhonePad  //显示便于聊天拨号的虚拟键盘
  textF.keyboardType = UIKeyboardType.EmailAddress  //显示便于输入Email的虚拟键盘
  textF.keyboardType = UIKeyboardType.DecimalPad  //显示用于输入数字和小数点的虚拟键盘
  textF.keyboardType = UIKeyboardType.Twitter  //显示方便些Twitter的虚拟键盘
  textF.keyboardType = UIKeyboardType.WebSearch  //显示便于在网页上书写的虚拟键盘
2.键盘的外观
  textF.keyboardAppearance = .Default //默认外观:浅灰色
  textF.keyboardAppearance = .Dark //黑色
  textF.keyboardAppearance = .Light //亮色,与Default很相似
3.键盘的回车键
  textF.returnKeyType = .Default //默认:灰色按钮,标有Return
  textF.returnKeyType = .Continue  // 标有Continue的蓝色按钮 
  textF.returnKeyType = .Done //标有Done的蓝色按钮 
  textF.returnKeyType = .EmergencyCall //紧急呼叫按钮
  textF.returnKeyType = .Go  //标有Go的蓝色按钮 
  textF.returnKeyType = .Google  //标有Google的蓝色按钮,用于搜索
  textF.returnKeyType = .Join //标有Join的蓝色按钮 
  textF.returnKeyType = .Next //标有Next的蓝色按钮  
  textF.returnKeyType = .Route //标有Route的蓝色按钮
  textF.returnKeyType = .Search //标有Search的蓝色按钮
  textF.returnKeyType = .Send //标有Send的蓝色按钮
  textF.returnKeyType = .Yahoo //标有Yahoo!的蓝色按钮,用于搜索
4.键盘的大写属性
  textF.autocorrectionType = .Default  //默认
  textF.autocorrectionType = .No  //不自动更正
  textF.autocorrectionType = .Yes  //自动更正
5.安全性

当我们想输入密码的时候当然需要安全显示啦,键盘也为我们提供了这样一个属性。

  textF.secureTextEntry = true

二、键盘的事件分析

class ViewController: UIViewController,UITextFieldDelegate {

      var textField:UITextField?
     //Mark:life cycle
      override func viewDidLoad() {
         super.viewDidLoad()

         textField = UITextField(frame: CGRectMake(50, 50, 150, 30))
         textField?.backgroundColor = UIColor.cyanColor()
         textField?.textColor = UIColor.blackColor()
         textField?.placeholder = "请输入用户名"
         textField?.delegate = self
         view.addSubview(textField!)

      }
      override func viewWillDisappear(animated: Bool) {
         if self.textField != nil {
            let center = NSNotificationCenter.defaultCenter()
            center.removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
            center.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
         }
      }
      //Mark:Delegate
      func textFieldDidBeginEditing(textField: UITextField) {
         //监听键盘弹出通知
         NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
         //监听键盘收回通知
         NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
      }
      func textFieldDidEndEditing(textField: UITextField) {
         self.view.endEditing(true)
     }
     //键盘显示
      func keyboardWillShow(notification:NSNotification) {
         let textMaxY = CGRectGetMaxY(self.textField!.frame)
         let keyboardH : CGFloat = (notification.userInfo![UIKeyboardFrameEndUserInfoKey]?.CGRectValue.size.height)!
         let keyboardY : CGFloat = self.view.frame.size.height - keyboardH
         var duration  = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey]?.doubleValue
         if duration < 0.0 {
              duration = 0.25
         }
         UIView.animateWithDuration(duration!) { () -> Void in
             if (textMaxY > keyboardY) {
                 self.view.transform = CGAffineTransformMakeTranslation(0, keyboardY - textMaxY - 10)
             }else{
                 self.view.transform = CGAffineTransformIdentity
             }
          }

       }
      //键盘隐藏
      func keyboardWillHide(notification:NSNotification){
         let duration  = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey]?.doubleValue
         UIView.animateWithDuration(duration!) { () -> Void in
             self.view.transform = CGAffineTransformIdentity
         }
       }
 } 
实现方法二:
  func keyboardWillShow(note: NSNotification) {
    let userInfo = note.userInfo!
    let  keyBoardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
    
    let deltaY = keyBoardBounds.size.height - 200
    let animations:(() -> Void) = {
        if deviceTypeIphone5() {
            //键盘的偏移量
            self.textView.transform = CGAffineTransform(translationX: 0 , y: -deltaY)
        }
    }
    
    if duration > 0 {
        let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
        
        UIView.animate(withDuration: duration, delay: 0, options:options, animations: animations, completion: nil)
        
    }else{
        animations()
    }
}


func keyboardWillHidden(note: NSNotification) {
    self.textView.transform = CGAffineTransform(translationX: 0 , y: 0)
}
在这里对代码进行简单的解释:

我们都知道,当焦点到可输入的控件上(一般指UITextField)时,键盘会自动显示,并且触发UIKeyboardWillShowNotification通知;当焦点离开可输入的控件时,键盘会自动隐藏,并且触发UIKeyboardWillHideNotification通知。所以,要注册这两种通知事件。
关于notification.userInfo的作用就是返回许多有用的参数,在这里我用的是UIKeyboardAnimationDurationUserInfoKey,查看内部我们可以发现 UIKeyboardAnimationDurationUserInfoKey = "0.25";

你可能感兴趣的:(Swift基础之关于键盘的那些事儿)