No Description

ActionSheetAppearAnimator.swift 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // ActionSheetAppearAnimator.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 ActionSheetAppearAnimator: NSObject {
  10. weak var delegate: AlertAnimatorDelegate?
  11. func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
  12. return 0.3
  13. }
  14. func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
  15. guard let toVC = transitionContext.viewController(forKey: .to),
  16. let toView = toVC.view,
  17. let contentView = delegate?.getContentView() else { return }
  18. transitionContext.containerView.addSubview(toView)
  19. let duration = transitionDuration(using: transitionContext)
  20. let animation = CABasicAnimation(keyPath: "backgroundColor")
  21. animation.duration = duration
  22. animation.isRemovedOnCompletion = true
  23. animation.toValue = UIColor(red: 52 / 255,
  24. green: 52 / 255,
  25. blue: 52 / 255,
  26. alpha: 0.7).cgColor
  27. animation.fromValue = UIColor(red: 52 / 255,
  28. green: 52 / 255,
  29. blue: 52 / 255,
  30. alpha: 0).cgColor
  31. toView.layer.add(animation, forKey: nil)
  32. let contentAnimation = CATransition()
  33. contentAnimation.duration = duration
  34. contentAnimation.type = kCATransitionMoveIn
  35. contentAnimation.isRemovedOnCompletion = true
  36. contentAnimation.subtype = kCATransitionFromTop
  37. contentView.layer.add(contentAnimation, forKey: nil)
  38. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
  39. let isCancelled = transitionContext.transitionWasCancelled
  40. transitionContext.completeTransition(!isCancelled)
  41. }
  42. }
  43. }