BannerPositionFrame.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2017-2018 Dalton Hinterscher
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  9. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  10. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  11. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  12. */
  13. import UIKit
  14. @objc
  15. public enum BannerPosition: Int {
  16. case bottom
  17. case top
  18. }
  19. class BannerPositionFrame: NSObject {
  20. private(set) var startFrame: CGRect!
  21. private(set) var endFrame: CGRect!
  22. init(bannerPosition: BannerPosition,
  23. bannerWidth: CGFloat,
  24. bannerHeight: CGFloat,
  25. maxY: CGFloat,
  26. finishYOffset: CGFloat = 0,
  27. edgeInsets: UIEdgeInsets?) {
  28. super.init()
  29. self.startFrame = startFrame(for: bannerPosition, bannerWidth: bannerWidth, bannerHeight: bannerHeight, maxY: maxY, edgeInsets: edgeInsets)
  30. self.endFrame = endFrame(for: bannerPosition, bannerWidth: bannerWidth, bannerHeight: bannerHeight, maxY: maxY, finishYOffset: finishYOffset, edgeInsets: edgeInsets)
  31. }
  32. /**
  33. Returns the start frame for the notification banner based on the given banner position
  34. - parameter bannerPosition: The position the notification banners should slide in from
  35. - parameter bannerWidth: The width of the notification banner
  36. - parameter bannerHeight: The height of the notification banner
  37. - parameter maxY: The maximum `y` position the banner can slide in from. This value is only used
  38. if the bannerPosition is .bottom
  39. - parameter edgeInsets: The sides edges insets from superview
  40. */
  41. private func startFrame(for bannerPosition: BannerPosition,
  42. bannerWidth: CGFloat,
  43. bannerHeight: CGFloat,
  44. maxY: CGFloat,
  45. edgeInsets: UIEdgeInsets?) -> CGRect {
  46. let edgeInsets = edgeInsets ?? .zero
  47. switch bannerPosition {
  48. case .bottom:
  49. return CGRect(x: edgeInsets.left,
  50. y: maxY,
  51. width: bannerWidth - edgeInsets.left - edgeInsets.right,
  52. height: bannerHeight)
  53. case .top:
  54. return CGRect(x: edgeInsets.left,
  55. y: -bannerHeight,
  56. width: bannerWidth - edgeInsets.left - edgeInsets.right,
  57. height: bannerHeight)
  58. }
  59. }
  60. /**
  61. Returns the end frame for the notification banner based on the given banner position
  62. - parameter bannerPosition: The position the notification banners should slide in from
  63. - parameter bannerWidth: The width of the notification banner
  64. - parameter bannerHeight: The height of the notification banner
  65. - parameter maxY: The maximum `y` position the banner can slide in from. This value is only used if the bannerPosition is .bottom
  66. - parameter finishYOffset: The `y` position offset the banner can slide in. Used for displaying several banenrs simaltaneously
  67. - parameter edgeInsets: The sides edges insets from superview
  68. */
  69. private func endFrame(for bannerPosition: BannerPosition,
  70. bannerWidth: CGFloat,
  71. bannerHeight: CGFloat,
  72. maxY: CGFloat,
  73. finishYOffset: CGFloat = 0,
  74. edgeInsets: UIEdgeInsets?) -> CGRect {
  75. let edgeInsets = edgeInsets ?? .zero
  76. switch bannerPosition {
  77. case .bottom:
  78. return CGRect(x: edgeInsets.left,
  79. y: maxY - bannerHeight - edgeInsets.bottom - finishYOffset,
  80. width: startFrame.width,
  81. height: startFrame.height)
  82. case .top:
  83. return CGRect(x: edgeInsets.left,
  84. y: edgeInsets.top + finishYOffset,
  85. width: startFrame.width,
  86. height: startFrame.height)
  87. }
  88. }
  89. }