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