分类 键盘事件 下的文章

注册界面

注意点:移动uiview的,键盘事件监听和使用,自定义inputAccessoryView

xib部分代码
//
//  TextFiledKeyboardAccessory.swift
//  登陆排版界面
//
//  Created by zhang on 16/1/17.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit
@objc protocol TextFiledKeyboardAccessoryDelagate
{
    optional func textFiledKeyboardAccessoryPreClicked(sender: UIBarButtonItem,other:UIBarButtonItem)
    optional func textFiledKeyboardAccessoryNextClicked(sender: UIBarButtonItem,other:UIBarButtonItem)
    optional func textFiledKeyboardAccessoryDownClicked()
}
class TextFiledKeyboardAccessory: UIToolbar {
    weak var accessorydDelegate:TextFiledKeyboardAccessoryDelagate!

    @IBOutlet weak var preButton: UIBarButtonItem!

    @IBOutlet weak var nextButton: UIBarButtonItem!
    class func instance()->TextFiledKeyboardAccessory
    {
        return NSBundle.mainBundle().loadNibNamed("TextFiledKeyboardAccessory", owner: nil, options: nil).last as! TextFiledKeyboardAccessory
    }
    @IBAction func preClicked(sender: UIBarButtonItem) {
        self.accessorydDelegate?.textFiledKeyboardAccessoryPreClicked?(sender,other: self.nextButton)
    }
    @IBAction func nexClicked(sender: UIBarButtonItem) {
        self.accessorydDelegate?.textFiledKeyboardAccessoryNextClicked?(sender,other: self.preButton)
    }
    @IBAction func downClicked() {
        self.accessorydDelegate?.textFiledKeyboardAccessoryDownClicked?()
    }

}
控制器代码
//
//  ViewController.swift
//  登陆排版界面
//
//  Created by zhang on 16/1/17.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class ViewController: UIViewController,TextFiledKeyboardAccessoryDelagate,UITextFieldDelegate {

    // 存放登陆空间的view
    @IBOutlet weak var buttonView: UIView!
    // 懒加载所有的文本框
    lazy var textFields:[UITextField] =
    {
        let subViews:[UIView] = self.buttonView.subviews
        var textFields:[UITextField] = []
        subViews.forEach{
            if $0.tag == 3
            {
                textFields.append($0 as! UITextField)
            }
        }
        return textFields
    }()
    // 懒加载view
    lazy var textFiledKeyboardAccessory = TextFiledKeyboardAccessory.instance()
    // 当前活动获得焦点的 TextField 在对象数组中的下标
    var activeTextFieldIndex = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        // 循环设置 TextField 的代理和键盘 accessory
        self.textFields.forEach{
            self.textFiledKeyboardAccessory.accessorydDelegate = self
            $0.inputAccessoryView = self.textFiledKeyboardAccessory
            $0.delegate = self
        }
//        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
        // 监听键盘时间
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardShow:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardHide:", name: UIKeyboardWillHideNotification, object: nil)
    }
    // 代理方法,收回键盘
    func textFiledKeyboardAccessoryDownClicked() {
        self.view.endEditing(true)
    }
    // 代理方法,下一个文本框
    func textFiledKeyboardAccessoryNextClicked(sender: UIBarButtonItem,other:UIBarButtonItem) {
        self.activeTextFieldIndex = self.activeTextFieldIndex + 1
        self.textFields[self.activeTextFieldIndex].becomeFirstResponder()
        if self.activeTextFieldIndex == self.textFields.count - 1
        {
            sender.enabled = false
        }
        other.enabled = self.activeTextFieldIndex != 0
    }
    // 上一个文本框
    func textFiledKeyboardAccessoryPreClicked(sender: UIBarButtonItem,other:UIBarButtonItem) {
        self.activeTextFieldIndex = self.activeTextFieldIndex - 1
        // 设置当前获得焦点的文本框
        self.textFields[self.activeTextFieldIndex].becomeFirstResponder()
        if self.activeTextFieldIndex == 0
        {
            sender.enabled = false
        }
        other.enabled = self.activeTextFieldIndex < self.textFields.count - 1
    }
    // 设置当前获得窗口的下标
    func textFieldDidBeginEditing(textField: UITextField) {
        self.activeTextFieldIndex = (self.textFields as NSArray).indexOfObject(textField)
    }
    // 键盘出现时响应的时间,用户改变view的位置
    func keyboardShow(notification:NSNotification)
    {
        // 获得改变位置之后的位置信息
        let endFrame:CGRect = ((notification.userInfo!)["UIKeyboardFrameEndUserInfoKey"]!).CGRectValue

        let activeMaxY = CGRectGetMaxY(self.textFields[self.activeTextFieldIndex].frame)
        let activeMinY = CGRectGetMinY(self.textFields[self.activeTextFieldIndex].frame)

        if activeMaxY > endFrame.origin.y
        {
            self.buttonView.transform = CGAffineTransformMakeTranslation(0, endFrame.origin.y - activeMaxY)
        }
        if -self.buttonView.transform.ty > activeMinY
        {
            self.buttonView.transform = CGAffineTransformMakeTranslation(0, 0 - activeMinY)
        }
//[UIKeyboardFrameBeginUserInfoKey: NSRect: {{0, 667}, {375, 302}}, UIKeyboardCenterEndUserInfoKey: NSPoint: {187.5, 516}, UIKeyboardBoundsUserInfoKey: NSRect: {{0, 0}, {375, 302}}, UIKeyboardFrameEndUserInfoKey: NSRect: {{0, 365}, {375, 302}}, UIKeyboardAnimationDurationUserInfoKey: 0.25, UIKeyboardCenterBeginUserInfoKey: NSPoint: {187.5, 818}, UIKeyboardAnimationCurveUserInfoKey: 7, UIKeyboardIsLocalUserInfoKey: 1]
    }
    // 键盘收回时候执行的方法
    func keyboardHide(notification:NSNotification)
    {
        self.buttonView.transform = CGAffineTransformMakeTranslation(0, 0)
    }
}