ModalSize.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // ModalSize.swift
  3. // Presentr
  4. //
  5. // Created by Daniel Lozano on 7/6/16.
  6. // Copyright © 2016 danielozano. All rights reserved.
  7. //
  8. import UIKit
  9. /**
  10. Descibes a presented modal's size dimension (width or height). It is meant to be non-specific, but the exact position can be calculated by calling the 'calculate' methods, passing in the 'parentSize' which only the Presentation Controller should be aware of.
  11. - Default: Default size. Will use Presentr's default margins to calculate size of presented controller. This is the size the .Popup presentation type uses.
  12. - Half: Half of the screen.
  13. - Full: Full screen.
  14. - Custom: Custom fixed size.
  15. - Fluid: Custom percentage-based fluid size.
  16. - SideMargin: Uses side margins to calculate size.
  17. */
  18. public enum ModalSize {
  19. case `default`
  20. case half
  21. case full
  22. case custom(size: Float)
  23. case fluid(percentage: Float)
  24. case sideMargin(value: Float)
  25. /**
  26. Calculates the exact width value for the presented view controller.
  27. - parameter parentSize: The presenting view controller's size. Provided by the presentation controller.
  28. - returns: Exact float width value.
  29. */
  30. func calculateWidth(_ parentSize: CGSize) -> Float {
  31. switch self {
  32. case .default:
  33. return floorf(Float(parentSize.width) - (PresentrConstants.Values.defaultSideMargin * 2.0))
  34. case .half:
  35. return floorf(Float(parentSize.width) / 2.0)
  36. case .full:
  37. return Float(parentSize.width)
  38. case .custom(let size):
  39. return size
  40. case .fluid(let percentage):
  41. return floorf(Float(parentSize.width) * percentage)
  42. case .sideMargin(let value):
  43. return floorf(Float(parentSize.width) - value * 2.0)
  44. }
  45. }
  46. /**
  47. Calculates the exact height value for the presented view controller.
  48. - parameter parentSize: The presenting view controller's size. Provided by the presentation controller.
  49. - returns: Exact float height value.
  50. */
  51. func calculateHeight(_ parentSize: CGSize) -> Float {
  52. switch self {
  53. case .default:
  54. return floorf(Float(parentSize.height) * PresentrConstants.Values.defaultHeightPercentage)
  55. case .half:
  56. return floorf(Float(parentSize.height) / 2.0)
  57. case .full:
  58. return Float(parentSize.height)
  59. case .custom(let size):
  60. return size
  61. case .fluid(let percentage):
  62. return floorf(Float(parentSize.height) * percentage)
  63. case .sideMargin(let value):
  64. return floorf(Float(parentSize.height) - value * 2)
  65. }
  66. }
  67. }