ModalCenterPosition.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // ModalCenterPosition.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. Describes the presented presented view controller's center position. It is meant to be non-specific, but we can use the 'calculatePoint' method when we want to calculate the exact point by passing in the 'containerBounds' rect that only the presentation controller should be aware of.
  11. - Center: Center of the screen.
  12. - TopCenter: Center of the top half of the screen.
  13. - BottomCenter: Center of the bottom half of the screen.
  14. - Custom: A custom center position using a CGPoint which represents the center point of the presented view controller.
  15. - Custom: A custom center position to be calculated, using a CGPoint which represents the origin of the presented view controller.
  16. */
  17. public enum ModalCenterPosition {
  18. case center
  19. case topCenter
  20. case bottomCenter
  21. case custom(centerPoint: CGPoint)
  22. case customOrigin(origin: CGPoint)
  23. /**
  24. Calculates the exact position for the presented view controller center.
  25. - parameter containerBounds: The container bounds the controller is being presented in.
  26. - returns: CGPoint representing the presented view controller's center point.
  27. */
  28. func calculateCenterPoint(_ containerFrame: CGRect) -> CGPoint? {
  29. switch self {
  30. case .center:
  31. return CGPoint(x: containerFrame.origin.x + (containerFrame.width / 2),
  32. y: containerFrame.origin.y + (containerFrame.height / 2))
  33. case .topCenter:
  34. return CGPoint(x: containerFrame.origin.x + (containerFrame.width / 2),
  35. y: containerFrame.origin.y + (containerFrame.height * (1 / 4) - 1))
  36. case .bottomCenter:
  37. return CGPoint(x: containerFrame.origin.x + (containerFrame.width / 2),
  38. y: containerFrame.origin.y + (containerFrame.height * (3 / 4)))
  39. case .custom(let point):
  40. return point
  41. case .customOrigin(_):
  42. return nil
  43. }
  44. }
  45. func calculateOrigin() -> CGPoint? {
  46. switch self {
  47. case .customOrigin(let origin):
  48. return origin
  49. default:
  50. return nil
  51. }
  52. }
  53. }