Brak opisu

DisappearAnimatedTransitioning.swift 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // DisappearAnimatedTransitioning.swift
  3. // PaiAi
  4. //
  5. // Created by ffib on 2018/12/17.
  6. // Copyright © 2018 yb. All rights reserved.
  7. //
  8. import UIKit
  9. class DisappearAnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning {
  10. weak var delegate: AlertAnimatorDelegate?
  11. fileprivate var animator: AlertViewAnimatable
  12. init(animator: AlertViewAnimatable) {
  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 contentView = delegate?.getContentView() else { return }
  25. let duration = transitionDuration(using: transitionContext)
  26. animator.fromViewPresentingAnimation(duration: duration, in: fromView)
  27. animator.contentViewDisappearAnimation(duration: duration, in: contentView)
  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. }