Use spring animation for presenting sheet

This commit is contained in:
Shadowfacts 2020-09-10 23:09:47 -04:00
parent 6926446c4e
commit ece0c5c887
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 10 additions and 6 deletions

View File

@ -31,22 +31,26 @@ 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 velocity = 1 / CGFloat(duration)
UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.65, 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)
})
}
}