Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. import UIKit
  2.  
  3. final public class KeyboardObserving {
  4.  
  5. public typealias ChangeSet = (keyboardRect: CGRect, animationCurve: UIView.AnimationOptions, duration: Double)
  6.  
  7. public var onKeyboardWillShow: ((ChangeSet) -> Void)?
  8. public var onKeyboardWillHide: ((ChangeSet) -> Void)?
  9.  
  10. public init() {
  11. registerKeyboardNotification()
  12. }
  13.  
  14. deinit {
  15. removeKeyboardObservers()
  16. }
  17.  
  18. public func removeKeyboardObservers() {
  19. NotificationCenter.default.removeObserver(self)
  20. }
  21.  
  22. public func registerKeyboardNotification() {
  23. removeKeyboardObservers() // this should prevent from observing keyboard change twice.
  24.  
  25. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  26. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  27. }
  28.  
  29. // MARK: - Keyboard
  30. // MARK: will show
  31. @objc private func keyboardWillShow(_ notification: Notification) {
  32. guard
  33. let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
  34. let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
  35. let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
  36. else { return }
  37.  
  38. onKeyboardWillShow?((keyboardRect, UIView.AnimationOptions(rawValue: curve.uintValue), duration.doubleValue))
  39. }
  40.  
  41. // MARK: will hide
  42. @objc private func keyboardWillHide(_ notification: Notification) {
  43. guard
  44. let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
  45. let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
  46. let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
  47. else { return }
  48.  
  49. onKeyboardWillShow?((keyboardRect, UIView.AnimationOptions(rawValue: curve.uintValue), duration.doubleValue))
  50. }
  51. }
  52.  
  53.  
  54. class ViewController: UIViewController {
  55.  
  56. private let keyboardObserving = KeyboardObserving()
  57.  
  58. override func viewDidLoad() {
  59. super.viewDidLoad()
  60. // Do any additional setup after loading the view, typically from a nib.
  61.  
  62. keyboardObserving.onKeyboardWillShow = { change in
  63. print("show", change)
  64. }
  65.  
  66. keyboardObserving.onKeyboardWillHide = { change in
  67. print("hide", change)
  68. }
  69. }
  70.  
  71. override func didReceiveMemoryWarning() {
  72. super.didReceiveMemoryWarning()
  73. // Dispose of any resources that can be recreated.
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement