NSNotification+Extensions.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // NSNotification+Extensions.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2019 Nathan Tannar.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. // Created by Nathan Tannar on 8/25/17.
  26. //
  27. import UIKit
  28. internal extension NSNotification {
  29. var event: KeyboardEvent {
  30. switch self.name {
  31. case UIResponder.keyboardWillShowNotification:
  32. return .willShow
  33. case UIResponder.keyboardDidShowNotification:
  34. return .didShow
  35. case UIResponder.keyboardWillHideNotification:
  36. return .willHide
  37. case UIResponder.keyboardDidHideNotification:
  38. return .didHide
  39. case UIResponder.keyboardWillChangeFrameNotification:
  40. return .willChangeFrame
  41. case UIResponder.keyboardDidChangeFrameNotification:
  42. return .didChangeFrame
  43. default:
  44. return .unknown
  45. }
  46. }
  47. var timeInterval: TimeInterval? {
  48. guard let value = userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber else { return nil }
  49. return TimeInterval(truncating: value)
  50. }
  51. var animationCurve: UIView.AnimationCurve? {
  52. guard let index = (userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber)?.intValue else { return nil }
  53. guard index >= 0 && index <= 3 else { return .linear }
  54. return UIView.AnimationCurve.init(rawValue: index) ?? .linear
  55. }
  56. var animationOptions: UIView.AnimationOptions {
  57. guard let curve = animationCurve else { return [] }
  58. switch curve {
  59. case .easeIn:
  60. return .curveEaseIn
  61. case .easeOut:
  62. return .curveEaseOut
  63. case .easeInOut:
  64. return .curveEaseInOut
  65. case .linear:
  66. return .curveLinear
  67. @unknown default:
  68. return .curveLinear
  69. }
  70. }
  71. var startFrame: CGRect? {
  72. return (userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
  73. }
  74. var endFrame: CGRect? {
  75. return (userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
  76. }
  77. var isForCurrentApp: Bool? {
  78. return (userInfo?[UIResponder.keyboardIsLocalUserInfoKey] as? NSNumber)?.boolValue
  79. }
  80. }