PresentationModalPosition.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // Created by Tom Baranes on 17/07/16.
  3. // Copyright © 2016 Jake Lin. All rights reserved.
  4. //
  5. import UIKit
  6. public enum PresentationModalPosition: IBEnum {
  7. case center
  8. case topCenter
  9. case bottomCenter
  10. case leftCenter
  11. case rightCenter
  12. case customCenter(centerPoint: CGPoint)
  13. case customOrigin(origin: CGPoint)
  14. func modalCenter(in containerFrame: CGRect, modalSize: CGSize) -> CGPoint? {
  15. let xCenter = containerFrame.origin.x + (containerFrame.width / 2)
  16. let yCenter = containerFrame.origin.y + (containerFrame.height / 2)
  17. switch self {
  18. case .center:
  19. return CGPoint(x: xCenter, y: yCenter)
  20. case .topCenter:
  21. return CGPoint(x: xCenter, y: containerFrame.minY + (modalSize.height / 2))
  22. case .bottomCenter:
  23. return CGPoint(x: xCenter, y: containerFrame.maxY - (modalSize.height / 2))
  24. case .leftCenter:
  25. return CGPoint(x: containerFrame.minX + (modalSize.width / 2), y: yCenter)
  26. case .rightCenter:
  27. return CGPoint(x: containerFrame.maxX - (modalSize.width / 2), y: yCenter)
  28. case let .customCenter(point):
  29. return point
  30. case .customOrigin:
  31. return nil
  32. }
  33. }
  34. func calculateOrigin() -> CGPoint? {
  35. switch self {
  36. case let .customOrigin(origin):
  37. return origin
  38. default:
  39. return nil
  40. }
  41. }
  42. }
  43. public extension PresentationModalPosition {
  44. /**
  45. Initializes a swift `PresentationModalPosition` with provided optional string
  46. - Parameter string: The optional string to be converted into `PresnetationModalPosition`.
  47. */
  48. init(string: String?) {
  49. guard let string = string else {
  50. self = .center
  51. return
  52. }
  53. guard let (name, params) = string.extractNameAndParams() else {
  54. self = .center
  55. return
  56. }
  57. let point = CGPoint(x: params.toDouble(0) ?? 0, y: params.toDouble(1) ?? 0)
  58. switch name {
  59. case "center":
  60. self = .center
  61. case "topcenter":
  62. self = .topCenter
  63. case "bottomcenter":
  64. self = .bottomCenter
  65. case "leftcenter":
  66. self = .leftCenter
  67. case "rightcenter":
  68. self = .rightCenter
  69. case "customcenter" where params.count > 2:
  70. self = .customCenter(centerPoint: point)
  71. case "customorigin" where params.count > 2:
  72. self = .customOrigin(origin: point)
  73. default:
  74. self = .center
  75. }
  76. }
  77. }