You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
3.4 KiB

6 years ago
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2016 Luke Zhao <me@lkzhao.com>
  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
  13. // all 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
  21. // THE SOFTWARE.
  22. import UIKit
  23. extension HeroTransition: UIViewControllerTransitioningDelegate {
  24. var interactiveTransitioning: UIViewControllerInteractiveTransitioning? {
  25. return forceNotInteractive ? nil : self
  26. }
  27. public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  28. guard !isTransitioning else { return nil }
  29. self.state = .notified
  30. self.isPresenting = true
  31. self.fromViewController = fromViewController ?? presenting
  32. self.toViewController = toViewController ?? presented
  33. return self
  34. }
  35. public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  36. guard !isTransitioning else { return nil }
  37. self.state = .notified
  38. self.isPresenting = false
  39. self.fromViewController = fromViewController ?? dismissed
  40. return self
  41. }
  42. public func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
  43. return interactiveTransitioning
  44. }
  45. public func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
  46. return interactiveTransitioning
  47. }
  48. }
  49. extension HeroTransition: UIViewControllerAnimatedTransitioning {
  50. public func animateTransition(using context: UIViewControllerContextTransitioning) {
  51. transitionContext = context
  52. fromViewController = fromViewController ?? context.viewController(forKey: .from)
  53. toViewController = toViewController ?? context.viewController(forKey: .to)
  54. transitionContainer = context.containerView
  55. start()
  56. }
  57. public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
  58. return 0.375 // doesn't matter, real duration will be calculated later
  59. }
  60. public func animationEnded(_ transitionCompleted: Bool) {
  61. self.state = .possible
  62. }
  63. }
  64. extension HeroTransition: UIViewControllerInteractiveTransitioning {
  65. public var wantsInteractiveStart: Bool {
  66. return true
  67. }
  68. public func startInteractiveTransition(_ transitionContext: UIViewControllerContextTransitioning) {
  69. animateTransition(using: transitionContext)
  70. }
  71. }