Nenhuma Descrição

PresentDisappearAnimatedTransitioning.swift 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // PresentDisappearAnimatedTransitioning.swift
  3. // PaiAi
  4. //
  5. // Created by FFIB on 2018/12/17.
  6. // Copyright © 2018 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. final class PresentDisappearAnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning {
  10. weak var delegate: PresentAnimatorDelegate?
  11. fileprivate var animator: PresentAnimatable
  12. init(animator: PresentAnimatable) {
  13. self.animator = animator
  14. super.init()
  15. }
  16. func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
  17. return 0.3
  18. }
  19. func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
  20. guard let fromVC = transitionContext.viewController(forKey: .from),
  21. let fromView = fromVC.view,
  22. let toVC = transitionContext.viewController(forKey: .to),
  23. let toView = toVC.view,
  24. let animationView = delegate?.animationView else { return }
  25. let duration = transitionDuration(using: transitionContext)
  26. animator.fromViewPresentingAnimation(duration: duration, in: fromView)
  27. animator.contentViewDisappearAnimation(duration: duration, in: animationView)
  28. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
  29. let isCancelled = transitionContext.transitionWasCancelled
  30. transitionContext.completeTransition(!isCancelled)
  31. transitionContext.containerView.addSubview(toView)
  32. fromView.removeFromSuperview()
  33. }
  34. }
  35. }