SheetController/Sources/SheetController/SheetContainerPresentationA...

54 lines
2.1 KiB
Swift

//
// SheetContainerPresentationAnimationController.swift
// SheetController
//
// Created by Shadowfacts on 9/24/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import UIKit
class SheetContainerPresentationAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.35
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let toVC = transitionContext.viewController(forKey: .to) as? SheetContainerViewController else {
fatalError()
}
toVC.dimmingView.isHidden = true
let finalFrame = transitionContext.finalFrame(for: toVC)
let dimmingView = UIView(frame: finalFrame)
dimmingView.backgroundColor = toVC.dimmingView.backgroundColor
dimmingView.alpha = 0
let container = transitionContext.containerView
container.addSubview(dimmingView)
container.addSubview(toVC.view)
toVC.view.transform = CGAffineTransform(translationX: 0, y: toVC.initialConstant)
let duration = transitionDuration(using: transitionContext)
UIView.animate(withDuration: duration, animations: {
// we animate the dimming view's frame so that it doesn't go under the content view, in case there's a transparent background
// we also extend the dimming view under any rounded corners the content view has
dimmingView.frame = CGRect(x: 0, y: 0, width: dimmingView.bounds.width, height: toVC.initialConstant + toVC.content.view.layer.cornerRadius)
dimmingView.alpha = toVC.dimmingView.alpha
toVC.view.transform = .identity
}, completion: { (finished) in
dimmingView.removeFromSuperview()
toVC.dimmingView.isHidden = false
toVC.view.frame = finalFrame
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}