From 7ac34efeabb5b5eb08fcf3d1235dbc9ca0441662 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 10 Sep 2020 23:09:47 -0400 Subject: [PATCH] Use spring animation for presenting sheet --- ...tContainerDismissAnimationController.swift | 5 +++-- ...ainerPresentationAnimationController.swift | 20 ++++++++++++------- .../SheetContainerViewController.swift | 5 ++++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Sources/SheetController/SheetContainerDismissAnimationController.swift b/Sources/SheetController/SheetContainerDismissAnimationController.swift index 8b359fc..3ad3426 100644 --- a/Sources/SheetController/SheetContainerDismissAnimationController.swift +++ b/Sources/SheetController/SheetContainerDismissAnimationController.swift @@ -11,7 +11,7 @@ import UIKit class SheetContainerDismissAnimationController: NSObject, UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { - return 0.35 + return 0.5 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { @@ -36,7 +36,8 @@ class SheetContainerDismissAnimationController: NSObject, UIViewControllerAnimat if let initialVelocity = fromVC.dismissAnimationInitialVelocity { let vector = CGVector(dx: initialVelocity, dy: 0) - let parameters = UISpringTimingParameters(dampingRatio: 1, initialVelocity: vector) + let damping = SheetContainerViewController.springDamping + let parameters = UISpringTimingParameters(dampingRatio: damping, initialVelocity: vector) animator = UIViewPropertyAnimator(duration: duration, timingParameters: parameters) } else { animator = UIViewPropertyAnimator(duration: duration, curve: .easeOut) diff --git a/Sources/SheetController/SheetContainerPresentationAnimationController.swift b/Sources/SheetController/SheetContainerPresentationAnimationController.swift index 1cc50b5..981f76f 100644 --- a/Sources/SheetController/SheetContainerPresentationAnimationController.swift +++ b/Sources/SheetController/SheetContainerPresentationAnimationController.swift @@ -11,7 +11,7 @@ import UIKit class SheetContainerPresentationAnimationController: NSObject, UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { - return 0.35 + return 0.5 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { @@ -31,22 +31,28 @@ class SheetContainerPresentationAnimationController: NSObject, UIViewControllerA container.addSubview(dimmingView) container.addSubview(toVC.view) - toVC.view.transform = CGAffineTransform(translationX: 0, y: toVC.initialConstant) + let initialConstant = toVC.initialConstant + toVC.topConstraint.constant = toVC.view.bounds.height + toVC.view.layoutIfNeeded() let duration = transitionDuration(using: transitionContext) - UIView.animate(withDuration: duration, animations: { + let damping = SheetContainerViewController.springDamping + let pointsPerSecond: CGFloat = 50 + let velocity = pointsPerSecond / initialConstant + UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: damping, initialSpringVelocity: velocity, options: []) { // 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.frame = CGRect(x: 0, y: 0, width: dimmingView.bounds.width, height: initialConstant + toVC.content.view.layer.cornerRadius) dimmingView.alpha = toVC.dimmingView.alpha - toVC.view.transform = .identity - }, completion: { (finished) in + toVC.topConstraint.constant = initialConstant + toVC.view.layoutIfNeeded() + } completion: { (finished) in dimmingView.removeFromSuperview() toVC.dimmingView.isHidden = false toVC.view.frame = finalFrame transitionContext.completeTransition(!transitionContext.transitionWasCancelled) - }) + } } diff --git a/Sources/SheetController/SheetContainerViewController.swift b/Sources/SheetController/SheetContainerViewController.swift index 242f196..538a012 100644 --- a/Sources/SheetController/SheetContainerViewController.swift +++ b/Sources/SheetController/SheetContainerViewController.swift @@ -31,6 +31,8 @@ public extension SheetContainerViewControllerDelegate { open class SheetContainerViewController: UIViewController { + internal static let springDamping: CGFloat = 0.75 + public weak var delegate: SheetContainerViewControllerDelegate? public let content: UIViewController @@ -224,7 +226,8 @@ open class SheetContainerViewController: UIViewController { if delegate?.sheetContainer(self, willSnapToDetent: springToDetent.0) ?? true { self.topConstraint.constant = springToDetent.1 - UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 0.75, initialSpringVelocity: springVelocity, animations: { + let damping = SheetContainerViewController.springDamping + UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: damping, initialSpringVelocity: springVelocity, animations: { self.view.layoutIfNeeded() self.dimmingView.alpha = lerp(springToDetent.1, min: self.topDetent.offset, max: self.bottomDetent.offset, from: self.maximumDimmingAlpha, to: self.minimumDimmingAlpha) }, completion: { (finished) in