Blame view

App/firstInput/VCInputId.swift 3.48 KB
d774f0637   Trịnh Văn Quân   fisrt comit
1
  import UIKit
1341bf603   Trịnh Văn Quân   version 1.1
2
  import GeneralUtils
d774f0637   Trịnh Văn Quân   fisrt comit
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  
  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()
defd9642e   Trịnh Văn Quân   version 1.2
17
18
19
20
21
          if let id = getVcRoot()?.id {
              edtId.text = id
          } else {
              edtId.text = PreferenceUtils.getString(key: Constants.PreferenceKeys.ID)
          }
d774f0637   Trịnh Văn Quân   fisrt comit
22
23
24
      }
  
      @IBAction func btnUnknowClick(_ sender: Any) {
defd9642e   Trịnh Văn Quân   version 1.2
25
          _ = getVcRoot()?.changeCurrentController(VCSendTraoDoi.getInstance())
d774f0637   Trịnh Văn Quân   fisrt comit
26
27
28
      }
  
      @IBAction func btnNextClick(_ sender: Any) {
defd9642e   Trịnh Văn Quân   version 1.2
29
30
31
32
33
34
          if let id: String = edtId.text?.trimAndReturn(), id.length > 0 {
              PreferenceUtils.saveString(key: Constants.PreferenceKeys.ID, value: id)
              _ = getVcRoot()?.changeCurrentController(VCHome.getInstance())
          } else {
              CommonUtils.showToastLong(text: LocalizedString("khong_de_trong"))
          }
d774f0637   Trịnh Văn Quân   fisrt comit
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
      }
  
      @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
  }