85 lines
3.4 KiB
Swift
85 lines
3.4 KiB
Swift
//
|
|
// SheetContainerDismissAnimationController.swift
|
|
// SheetController
|
|
//
|
|
// Created by Shadowfacts on 9/24/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class SheetContainerDismissAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
|
|
|
|
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
|
|
if UIAccessibility.prefersCrossFadeTransitionsBackwardsCompat && (transitionContext?.viewController(forKey: .from) as? SheetContainerViewController)?.dismissAnimationInitialVelocity == nil {
|
|
return 0.25
|
|
} else {
|
|
return 0.35
|
|
}
|
|
}
|
|
|
|
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
|
|
guard let fromVC = transitionContext.viewController(forKey: .from) as? SheetContainerViewController else {
|
|
fatalError()
|
|
}
|
|
|
|
if UIAccessibility.prefersCrossFadeTransitionsBackwardsCompat && fromVC.dismissAnimationInitialVelocity == nil {
|
|
animateCrossFadeTransition(using: transitionContext)
|
|
return
|
|
}
|
|
|
|
fromVC.view.alpha = 1.0
|
|
fromVC.dimmingView.isHidden = true
|
|
|
|
// match the frame to the original dimming view's frame so that it doesn't go under the content
|
|
let dimmingView = UIView(frame: fromVC.dimmingView.bounds)
|
|
dimmingView.backgroundColor = fromVC.dimmingView.backgroundColor
|
|
dimmingView.alpha = fromVC.dimmingView.alpha
|
|
|
|
let container = transitionContext.containerView
|
|
container.addSubview(dimmingView)
|
|
container.addSubview(fromVC.view)
|
|
|
|
let duration = transitionDuration(using: transitionContext)
|
|
let animator: UIViewPropertyAnimator
|
|
|
|
if let initialVelocity = fromVC.dismissAnimationInitialVelocity {
|
|
let vector = CGVector(dx: initialVelocity, dy: 0)
|
|
let damping = SheetContainerViewController.springDamping
|
|
let parameters = UISpringTimingParameters(dampingRatio: damping, initialVelocity: vector)
|
|
animator = UIViewPropertyAnimator(duration: duration, timingParameters: parameters)
|
|
} else {
|
|
animator = UIViewPropertyAnimator(duration: duration, curve: .easeInOut)
|
|
}
|
|
|
|
animator.addAnimations {
|
|
dimmingView.frame = container.bounds
|
|
dimmingView.alpha = 0
|
|
fromVC.view.transform = CGAffineTransform(translationX: 0, y: fromVC.content.view.bounds.height)
|
|
}
|
|
animator.addCompletion { (position) in
|
|
dimmingView.removeFromSuperview()
|
|
|
|
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
|
|
}
|
|
|
|
animator.startAnimation()
|
|
}
|
|
|
|
func animateCrossFadeTransition(using transitionContext: UIViewControllerContextTransitioning) {
|
|
guard let fromVC = transitionContext.viewController(forKey: .from) as? SheetContainerViewController else {
|
|
fatalError()
|
|
}
|
|
|
|
transitionContext.containerView.addSubview(fromVC.view)
|
|
|
|
let duration = transitionDuration(using: transitionContext)
|
|
UIView.animate(withDuration: duration) {
|
|
fromVC.view.alpha = 0
|
|
} completion: { (finished) in
|
|
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
|
|
}
|
|
}
|
|
|
|
}
|