// // ActionSheetAppearAnimator.swift // PaiAi // // Created by ffib on 2018/12/17. // Copyright © 2018 yb. All rights reserved. // import UIKit class ActionSheetAppearAnimator: NSObject { weak var delegate: AlertAnimatorDelegate? func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.3 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { guard let toVC = transitionContext.viewController(forKey: .to), let toView = toVC.view, let contentView = delegate?.getContentView() else { return } transitionContext.containerView.addSubview(toView) let duration = transitionDuration(using: transitionContext) let animation = CABasicAnimation(keyPath: "backgroundColor") animation.duration = duration animation.isRemovedOnCompletion = true animation.toValue = UIColor(red: 52 / 255, green: 52 / 255, blue: 52 / 255, alpha: 0.7).cgColor animation.fromValue = UIColor(red: 52 / 255, green: 52 / 255, blue: 52 / 255, alpha: 0).cgColor toView.layer.add(animation, forKey: nil) let contentAnimation = CATransition() contentAnimation.duration = duration contentAnimation.type = kCATransitionMoveIn contentAnimation.isRemovedOnCompletion = true contentAnimation.subtype = kCATransitionFromTop contentView.layer.add(contentAnimation, forKey: nil) DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) { let isCancelled = transitionContext.transitionWasCancelled transitionContext.completeTransition(!isCancelled) } } }