VCInputId.swift 2.99 KB
import UIKit

class VCInputId: UIViewController {
    @IBOutlet weak var edtId: UITextField!
    @IBOutlet weak var topMargin: NSLayoutConstraint!

    static func getInstance() -> VCInputId {
        let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vcOpen = storyboard.instantiateViewController(withIdentifier: "VCInputId") as! VCInputId
        return vcOpen
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        registerKeyBoardEvent()
        edtId.text = getVcRoot()?.id
    }

    @IBAction func btnUnknowClick(_ sender: Any) {
    }

    @IBAction func btnNextClick(_ sender: Any) {
        _ = getVcRoot()?.changeCurrentController(VCSendTraoDoi.getInstance())
    }

    @IBAction func edtActionTrigerClick(_ sender: Any) {
        edtId.endEditing(true)
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        unregisterKeyBoardEvent()
    }

    func getVcRoot() -> VCRoot? {
        return self.parent as? VCRoot
    }

    //region ======= keyboard process ====
    private func registerKeyBoardEvent() {
        NotificationCenter.default.setObserver(self, selector: #selector(VCInputId.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow.rawValue, object: nil);
        NotificationCenter.default.setObserver(self, selector: #selector(VCInputId.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide.rawValue, object: nil);
    }

    private func unregisterKeyBoardEvent() {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func keyboardWillShow(_ notification: Notification) {
        guard let userInfo = notification.userInfo else {
            return
        }
        guard let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue else {
            return
        }
        guard let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue else {
            return
        }
        topMargin.constant = 50 - keyboardFrame.height
        UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.5, options: .curveLinear, animations: { () -> Void in
            self.view.layoutIfNeeded()
        }, completion: nil)
    }

    func keyboardWillHide(_ notification: Notification) {
        guard let userInfo = notification.userInfo else {
            return
        }
        guard let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue else {
            return
        }
        topMargin.constant = 50
        UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.5, options: .curveLinear, animations: { () -> Void in
            self.view.layoutIfNeeded()
        }, completion: nil)
    }
    //endregion
}