AnimatableModalViewController.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // Created by Tom Baranes on 16/07/16.
  3. // Copyright © 2016 Jake Lin. All rights reserved.
  4. //
  5. import UIKit
  6. /// `AnimatableModalViewController` is a customised modal view controller used as the `presentedViewController` for `UIPresentationController`. We can use it in Interface Builder to style the modal view and dimming view. Also configure the presentation and dismissal animations.
  7. open class AnimatableModalViewController: UIViewController, PresentationDesignable {
  8. // MARK: - AnimatablePresentationController
  9. public var contextFrameForPresentation: (() -> CGRect)? {
  10. didSet {
  11. configurePresenterFrameForPresentation()
  12. }
  13. }
  14. @IBInspectable var _presentationAnimationType: String? {
  15. didSet {
  16. if let animationType = PresentationAnimationType(string: _presentationAnimationType) {
  17. presentationAnimationType = animationType
  18. }
  19. }
  20. }
  21. public var presentationAnimationType: PresentationAnimationType = .cover(from: .bottom) {
  22. didSet {
  23. if oldValue.stringValue != presentationAnimationType.stringValue {
  24. configurePresenter()
  25. }
  26. }
  27. }
  28. @IBInspectable var _dismissalAnimationType: String? {
  29. didSet {
  30. if let animationType = PresentationAnimationType(string: _dismissalAnimationType) {
  31. dismissalAnimationType = animationType
  32. }
  33. }
  34. }
  35. public var dismissalAnimationType: PresentationAnimationType = .cover(from: .bottom) {
  36. didSet {
  37. if oldValue.stringValue != dismissalAnimationType.stringValue {
  38. configurePresenter()
  39. }
  40. }
  41. }
  42. @IBInspectable public var transitionDuration: Double = .nan {
  43. didSet {
  44. presenter?.transitionDuration = transitionDuration
  45. }
  46. }
  47. @IBInspectable var _modalPosition: String? {
  48. didSet {
  49. modalPosition = PresentationModalPosition(string: _modalPosition ?? "")
  50. }
  51. }
  52. public var modalPosition: PresentationModalPosition = .center {
  53. didSet {
  54. presenter?.presentationConfiguration?.modalPosition = modalPosition
  55. }
  56. }
  57. @IBInspectable var _modalWidth: String? {
  58. didSet {
  59. let modalWidth = PresentationModalSize(string: _modalWidth) ?? .default
  60. modalSize = (modalWidth, modalSize.height)
  61. }
  62. }
  63. @IBInspectable var _modalHeight: String? {
  64. didSet {
  65. let modalHeight = PresentationModalSize(string: _modalHeight) ?? .default
  66. modalSize = (modalSize.width, modalHeight)
  67. }
  68. }
  69. public var modalSize: ModalSize = (.half, .half) {
  70. didSet {
  71. presenter?.presentationConfiguration?.modalSize = modalSize
  72. }
  73. }
  74. @IBInspectable public var cornerRadius: CGFloat = .nan {
  75. didSet {
  76. presenter?.presentationConfiguration?.cornerRadius = cornerRadius
  77. }
  78. }
  79. @IBInspectable public var dismissOnTap: Bool = true {
  80. didSet {
  81. presenter?.presentationConfiguration?.dismissOnTap = dismissOnTap
  82. }
  83. }
  84. @IBInspectable public var backgroundColor: UIColor = .black {
  85. didSet {
  86. presenter?.presentationConfiguration?.backgroundColor = backgroundColor
  87. }
  88. }
  89. @IBInspectable public var opacity: CGFloat = 0.7 {
  90. didSet {
  91. presenter?.presentationConfiguration?.opacity = opacity
  92. }
  93. }
  94. /// The blur effect style of the dimming view. If use this property, `backgroundColor` and `opacity` are ignored.
  95. open var blurEffectStyle: UIBlurEffect.Style? {
  96. didSet {
  97. presenter?.presentationConfiguration?.blurEffectStyle = blurEffectStyle
  98. }
  99. }
  100. @IBInspectable var _blurEffectStyle: String? {
  101. didSet {
  102. blurEffectStyle = UIBlurEffect.Style(string: _blurEffectStyle)
  103. }
  104. }
  105. @IBInspectable public var blurOpacity: CGFloat = .nan {
  106. didSet {
  107. presenter?.presentationConfiguration?.blurOpacity = blurOpacity
  108. }
  109. }
  110. @IBInspectable public var shadowColor: UIColor? {
  111. didSet {
  112. presenter?.presentationConfiguration?.shadowColor = shadowColor
  113. }
  114. }
  115. @IBInspectable public var shadowRadius: CGFloat = 0.7 {
  116. didSet {
  117. presenter?.presentationConfiguration?.shadowRadius = shadowRadius
  118. }
  119. }
  120. @IBInspectable public var shadowOpacity: CGFloat = CGFloat.nan {
  121. didSet {
  122. presenter?.presentationConfiguration?.shadowOpacity = shadowOpacity
  123. }
  124. }
  125. @IBInspectable public var shadowOffset: CGPoint = .zero {
  126. didSet {
  127. presenter?.presentationConfiguration?.shadowOffset = shadowOffset
  128. }
  129. }
  130. @IBInspectable var _keyboardTranslation: String = "" {
  131. didSet {
  132. keyboardTranslation = ModalKeyboardTranslation(string: _keyboardTranslation) ?? .none
  133. }
  134. }
  135. public var keyboardTranslation: ModalKeyboardTranslation = .none {
  136. didSet {
  137. presenter?.presentationConfiguration?.keyboardTranslation = keyboardTranslation
  138. }
  139. }
  140. public var presenter: PresentationPresenter?
  141. // MARK: Life cycle
  142. public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  143. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  144. configurePresenter()
  145. }
  146. public required init?(coder aDecoder: NSCoder) {
  147. super.init(coder: aDecoder)
  148. configurePresenter()
  149. }
  150. // MARK: Life cycle
  151. override open func viewDidLoad() {
  152. super.viewDidLoad()
  153. }
  154. open override func viewDidAppear(_ animated: Bool) {
  155. super.viewDidAppear(animated)
  156. configureDismissalTransition()
  157. }
  158. open override func viewDidLayoutSubviews() {
  159. super.viewDidLayoutSubviews()
  160. configurePresenterFrameForPresentation()
  161. }
  162. }