DismissAnimationController.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // DismissAnimationController.swift
  3. // ATGMediaBrowser
  4. //
  5. // Created by Suraj Thomas K on 7/19/18.
  6. // Copyright © 2018 Al Tayer Group LLC.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  9. // and associated documentation files (the "Software"), to deal in the Software without
  10. // restriction, including without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  12. // Software is furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all copies or
  15. // substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  18. // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  20. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. internal class DismissAnimationController: NSObject {
  24. private enum Constants {
  25. static let minimumVelocity: CGFloat = 15.0
  26. static let minimumTranslation: CGFloat = 0.25
  27. static let transitionDuration = 0.3
  28. static let updateFrameRate: CGFloat = 60.0
  29. static let transitionSpeedFactor: CGFloat = 0.15
  30. static let minimumZoomDuringInteraction: CGFloat = 0.9
  31. }
  32. internal var image: UIImage?
  33. internal let gestureDirection: MediaBrowserViewController.GestureDirection
  34. internal weak var viewController: MediaBrowserViewController?
  35. internal var interactionInProgress = false
  36. private lazy var imageView = UIImageView()
  37. private var timer: Timer?
  38. private var distanceToMove: CGPoint = .zero
  39. private var relativePosition: CGPoint = .zero
  40. private var progressValue: CGFloat {
  41. return (gestureDirection == .horizontal) ? relativePosition.y : relativePosition.x
  42. }
  43. private var shouldZoomOutOnInteraction = false
  44. init(
  45. image: UIImage? = nil,
  46. gestureDirection: MediaBrowserViewController.GestureDirection,
  47. viewController: MediaBrowserViewController
  48. ) {
  49. self.image = image
  50. self.gestureDirection = gestureDirection
  51. self.viewController = viewController
  52. }
  53. internal func handleInteractiveTransition(_ recognizer: UIPanGestureRecognizer) {
  54. let translation = recognizer.translation(in: recognizer.view)
  55. let progress = CGPoint(
  56. x: translation.x / UIScreen.main.bounds.size.width,
  57. y: translation.y / UIScreen.main.bounds.size.height
  58. )
  59. switch recognizer.state {
  60. case .began:
  61. beginTransition()
  62. fallthrough
  63. case .changed:
  64. relativePosition = progress
  65. updateTransition()
  66. case .ended, .cancelled, .failed:
  67. var toMove: CGFloat = 0.0
  68. if abs(progressValue) > Constants.minimumTranslation {
  69. if let viewController = viewController,
  70. let targetFrame = viewController.dataSource?.targetFrameForDismissal(viewController) {
  71. animateToTargetFrame(targetFrame)
  72. return
  73. } else {
  74. toMove = (progressValue / abs(progressValue))
  75. }
  76. } else {
  77. toMove = -progressValue
  78. }
  79. if gestureDirection == .horizontal {
  80. distanceToMove.x = -relativePosition.x
  81. distanceToMove.y = toMove
  82. } else {
  83. distanceToMove.x = toMove
  84. distanceToMove.y = -relativePosition.y
  85. }
  86. if timer == nil {
  87. timer = Timer.scheduledTimer(
  88. timeInterval: 1.0/Double(Constants.updateFrameRate),
  89. target: self,
  90. selector: #selector(update(_:)),
  91. userInfo: nil,
  92. repeats: true
  93. )
  94. }
  95. default:
  96. break
  97. }
  98. }
  99. internal func animateToTargetFrame(_ target: CGRect) {
  100. let frame = imageViewFrame(for: imageView.bounds.size, in: target, mode: .scaleAspectFill)
  101. UIView.animate(withDuration: Constants.transitionDuration, animations: {
  102. self.imageView.frame = frame
  103. }) { finished in
  104. if finished {
  105. self.interactionInProgress = false
  106. if self.gestureDirection == .horizontal {
  107. self.relativePosition.y = -1.0
  108. } else {
  109. self.relativePosition.x = -1.0
  110. }
  111. self.finishTransition()
  112. }
  113. }
  114. }
  115. @objc private func update(_ timeInterval: TimeInterval) {
  116. let speed = (Constants.updateFrameRate * Constants.transitionSpeedFactor)
  117. let xDistance = distanceToMove.x / speed
  118. let yDistance = distanceToMove.y / speed
  119. distanceToMove.x -= xDistance
  120. distanceToMove.y -= yDistance
  121. relativePosition.x += xDistance
  122. relativePosition.y += yDistance
  123. updateTransition()
  124. let translation = CGPoint(
  125. x: xDistance * (UIScreen.main.bounds.size.width),
  126. y: yDistance * (UIScreen.main.bounds.size.height)
  127. )
  128. let directionalTranslation = (gestureDirection == .horizontal) ? translation.y : translation.x
  129. if abs(directionalTranslation) < 1.0 {
  130. relativePosition.x += distanceToMove.x
  131. relativePosition.y += distanceToMove.y
  132. updateTransition()
  133. interactionInProgress = false
  134. finishTransition()
  135. }
  136. }
  137. internal func beginTransition() {
  138. shouldZoomOutOnInteraction = false
  139. if let viewController = viewController {
  140. shouldZoomOutOnInteraction = viewController.dataSource?.targetFrameForDismissal(viewController) != nil
  141. }
  142. createTransitionViews()
  143. viewController?.mediaContainerView.isHidden = true
  144. viewController?.hideControls = true
  145. }
  146. private func finishTransition() {
  147. distanceToMove = .zero
  148. timer?.invalidate()
  149. timer = nil
  150. imageView.removeFromSuperview()
  151. let directionalPosition = (gestureDirection == .horizontal) ? relativePosition.y : relativePosition.x
  152. if directionalPosition != 0.0 {
  153. viewController?.dismiss(animated: false, completion: nil)
  154. } else {
  155. viewController?.mediaContainerView.isHidden = false
  156. viewController?.hideControls = false
  157. }
  158. }
  159. private func createTransitionViews() {
  160. imageView.image = image
  161. imageView.frame = imageViewFrame(
  162. for: image?.size ?? .zero,
  163. in: viewController?.view.bounds ?? .zero
  164. )
  165. viewController?.view.addSubview(imageView)
  166. imageView.transform = CGAffineTransform.identity
  167. }
  168. private func updateTransition() {
  169. var transform = CGAffineTransform.identity
  170. let directionalPosition = (gestureDirection == .horizontal) ? relativePosition.y : relativePosition.x
  171. if shouldZoomOutOnInteraction {
  172. let scale = CGFloat.maximum(Constants.minimumZoomDuringInteraction, 1.0 - abs(directionalPosition))
  173. transform = transform.scaledBy(x: scale, y: scale)
  174. }
  175. if gestureDirection == .horizontal {
  176. transform = transform.translatedBy(
  177. x: shouldZoomOutOnInteraction ? relativePosition.x * UIScreen.main.bounds.size.width : 0.0,
  178. y: relativePosition.y * UIScreen.main.bounds.size.height
  179. )
  180. } else {
  181. transform = transform.translatedBy(
  182. x: relativePosition.x * UIScreen.main.bounds.size.width,
  183. y: shouldZoomOutOnInteraction ? relativePosition.y * UIScreen.main.bounds.size.height : 0.0
  184. )
  185. }
  186. imageView.transform = transform
  187. }
  188. private func imageViewFrame(for imageSize: CGSize, in frame: CGRect, mode: UIView.ContentMode = .scaleAspectFit) -> CGRect {
  189. guard imageSize != .zero,
  190. mode == .scaleAspectFit || mode == .scaleAspectFill else {
  191. return frame
  192. }
  193. var targetImageSize = frame.size
  194. let aspectHeight = frame.size.width / imageSize.width * imageSize.height
  195. let aspectWidth = frame.size.height / imageSize.height * imageSize.width
  196. if imageSize.width / imageSize.height > frame.size.width / frame.size.height {
  197. if mode == .scaleAspectFit {
  198. targetImageSize.height = aspectHeight
  199. } else {
  200. targetImageSize.width = aspectWidth
  201. }
  202. } else {
  203. if mode == .scaleAspectFit {
  204. targetImageSize.width = aspectWidth
  205. } else {
  206. targetImageSize.height = aspectHeight
  207. }
  208. }
  209. let x = frame.minX + (frame.size.width - targetImageSize.width) / 2.0
  210. let y = frame.minY + (frame.size.height - targetImageSize.height) / 2.0
  211. return CGRect(origin: CGPoint(x: x, y: y), size: targetImageSize)
  212. }
  213. }