ModalTransitionAnimator.swift 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // MIT License
  2. //
  3. // Copyright (c) 2021 Daniel Gauthier
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. //
  23. // From https://github.com/danielmgauthier/ViewControllerTransitionExample
  24. // SPDX-License-Identifier: MIT
  25. import UIKit
  26. class ModalTransitionAnimator: NSObject {
  27. private let presenting: Bool
  28. init(presenting: Bool) {
  29. self.presenting = presenting
  30. super.init()
  31. }
  32. }
  33. extension ModalTransitionAnimator: UIViewControllerAnimatedTransitioning {
  34. func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { 0.5 }
  35. func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
  36. if presenting {
  37. animatePresentation(using: transitionContext)
  38. } else {
  39. animateDismissal(using: transitionContext)
  40. }
  41. }
  42. private func animatePresentation(using transitionContext: UIViewControllerContextTransitioning) {
  43. let presentedViewController = transitionContext.viewController(forKey: .to)!
  44. transitionContext.containerView.addSubview(presentedViewController.view)
  45. let presentedFrame = transitionContext.finalFrame(for: presentedViewController)
  46. let dismissedFrame = CGRect(x: presentedFrame.minX, y: transitionContext.containerView.bounds.height, width: presentedFrame.width, height: presentedFrame.height)
  47. presentedViewController.view.frame = dismissedFrame
  48. let animator = UIViewPropertyAnimator(duration: transitionDuration(using: transitionContext), dampingRatio: 1.0) {
  49. presentedViewController.view.frame = presentedFrame
  50. }
  51. animator.addCompletion { _ in
  52. transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
  53. }
  54. animator.startAnimation()
  55. }
  56. private func animateDismissal(using transitionContext: UIViewControllerContextTransitioning) {
  57. let presentedViewController = transitionContext.viewController(forKey: .from)!
  58. let presentedFrame = transitionContext.finalFrame(for: presentedViewController)
  59. let dismissedFrame = CGRect(x: presentedFrame.minX, y: transitionContext.containerView.bounds.height, width: presentedFrame.width, height: presentedFrame.height)
  60. let animator = UIViewPropertyAnimator(duration: transitionDuration(using: transitionContext), dampingRatio: 1.0) {
  61. presentedViewController.view.frame = dismissedFrame
  62. }
  63. animator.addCompletion { _ in
  64. transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
  65. }
  66. animator.startAnimation()
  67. }
  68. }