PresentationDesignable.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Created by Tom Baranes on 16/07/16.
  3. // Copyright © 2016 Jake Lin. All rights reserved.
  4. //
  5. import UIKit
  6. public typealias ModalSize = (width: PresentationModalSize, height: PresentationModalSize)
  7. /// PresentationDesignable is a protocol to define customised modal view controller which is used as the `presentedViewController` for `UIPresentationController`
  8. public protocol PresentationDesignable: class {
  9. var presenter: PresentationPresenter? { get set }
  10. /// Frame of the presentingVC's dimmingView. Used that property if you want to simulate a presentation `overCurrentContext`. If nil, the dimmingView will be in fullscreen.
  11. var contextFrameForPresentation: (() -> CGRect)? { get set }
  12. /// Presentation animation type, all supported animation type can be found in `PresentationAnimationType`
  13. var presentationAnimationType: PresentationAnimationType { get set }
  14. /// Dismissal animation type, all supported animation type can be found in `PresentationAnimationType`
  15. var dismissalAnimationType: PresentationAnimationType { get set }
  16. /// Tranistion duration
  17. var transitionDuration: Double { get set }
  18. // Modal position, all supported modal position can be found in `PresentationModalPosition`
  19. var modalPosition: PresentationModalPosition { get set }
  20. // Modal Size, all supported modal Sizes can be found in `PresentationModalSize`
  21. var modalSize: ModalSize { get set }
  22. /// The corner radius of the modal view
  23. var cornerRadius: CGFloat { get set }
  24. /// If set to `true`, the modal view will dismiss when tap on the dimming view.
  25. var dismissOnTap: Bool { get set }
  26. /// The background color of the dimming view. The default value is black color.
  27. var backgroundColor: UIColor { get set }
  28. /// The opacity of the dimming view. The default value is `0.7`.
  29. var opacity: CGFloat { get set }
  30. /// The blur effect style of the dimming view. If use this property, `backgroundColor` and `opacity` are ignored.
  31. var blurEffectStyle: UIBlurEffect.Style? { get set }
  32. /// The blur opacity of the dimming view. If use this property, `backgroundColor` and `opacity` are ignored.
  33. var blurOpacity: CGFloat { get set }
  34. /// The shadow color of the modal view. If use this property, `cornerRadius` is ignored.
  35. var shadowColor: UIColor? { get set }
  36. /// The shadow radius of the modal view. If use this property, `cornerRadius` is ignored.
  37. var shadowRadius: CGFloat { get set }
  38. /// The shadow opacity of the modal view. If use this property, `cornerRadius` is ignored.
  39. var shadowOpacity: CGFloat { get set }
  40. /// The shadow offset of the modal view. If use this property, `cornerRadius` is ignored.
  41. var shadowOffset: CGPoint { get set }
  42. /// Modal's translation when the keyboard is opening
  43. var keyboardTranslation: ModalKeyboardTranslation { get set }
  44. }
  45. // MARK: - Configuration
  46. public extension PresentationDesignable where Self: UIViewController {
  47. func configurePresenter() {
  48. let presentationManager = PresentationPresenterManager.shared
  49. presenter = presentationManager.retrievePresenter(presentationAnimationType: presentationAnimationType,
  50. transitionDuration: transitionDuration)
  51. presenter?.dismissalAnimationType = dismissalAnimationType
  52. transitioningDelegate = presenter
  53. modalPresentationStyle = .custom
  54. if let systemTransition = presentationAnimationType.systemTransition {
  55. modalTransitionStyle = systemTransition
  56. }
  57. let presentationConfiguration = PresentationConfiguration()
  58. presentationConfiguration.contextFrameForPresentation = contextFrameForPresentation?()
  59. presentationConfiguration.modalPosition = modalPosition
  60. presentationConfiguration.modalSize = modalSize
  61. presentationConfiguration.cornerRadius = cornerRadius
  62. presentationConfiguration.dismissOnTap = dismissOnTap
  63. presentationConfiguration.backgroundColor = backgroundColor
  64. presentationConfiguration.opacity = opacity
  65. presentationConfiguration.blurEffectStyle = blurEffectStyle
  66. presentationConfiguration.blurOpacity = blurOpacity
  67. presentationConfiguration.shadowColor = shadowColor
  68. presentationConfiguration.shadowOpacity = shadowOpacity
  69. presentationConfiguration.shadowRadius = shadowRadius
  70. presentationConfiguration.shadowOffset = shadowOffset
  71. presentationConfiguration.keyboardTranslation = keyboardTranslation
  72. presenter?.presentationConfiguration = presentationConfiguration
  73. }
  74. func configureDismissalTransition() {
  75. let animationType = dismissalAnimationType
  76. if let dismissalSystemTransition = animationType.systemTransition {
  77. modalTransitionStyle = dismissalSystemTransition
  78. }
  79. }
  80. func configurePresenterFrameForPresentation() {
  81. presenter?.presentationConfiguration?.contextFrameForPresentation = contextFrameForPresentation?()
  82. }
  83. }
  84. // MARK: - PresentationConfiguration
  85. /// `PresentationConfiguration` a class is used for specifying the dimming view and modal view for `AnimatablePresentationController`
  86. public class PresentationConfiguration {
  87. public var cornerRadius: CGFloat = .nan
  88. public var dismissOnTap: Bool = true
  89. public var backgroundColor: UIColor = .black
  90. public var opacity: CGFloat = 0.7
  91. public var blurEffectStyle: UIBlurEffect.Style?
  92. public var blurOpacity: CGFloat = .nan
  93. public var shadowColor: UIColor?
  94. public var shadowRadius: CGFloat = .nan
  95. public var shadowOpacity: CGFloat = 0.7
  96. public var shadowOffset: CGPoint = .zero
  97. public var modalPosition: PresentationModalPosition = .center
  98. public var modalSize: (PresentationModalSize, PresentationModalSize) = (.half, .half)
  99. public var keyboardTranslation = ModalKeyboardTranslation.none
  100. public var contextFrameForPresentation: CGRect?
  101. }