This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ViewController: UIViewController { | |
| private let scrollView = UIScrollView() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| view.backgroundColor = .systemBackground | |
| setupScrollView() | |
| } | |
| private func setupScrollView() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BottomSheetTransition: UIPercentDrivenInteractiveTransition { | |
| private var dismissalAnimator: UIViewPropertyAnimator? | |
| private var presentationAnimator: UIViewPropertyAnimator? | |
| private let animationDuration = 0.5 | |
| private let dampingRatio: Double = 0.9 | |
| var dismissFractionComplete: CGFloat { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BottomSheetTransitioningDelegate: NSObject { | |
| let transition: BottomSheetTransition | |
| override init() { | |
| self.transition = BottomSheetTransition() | |
| } | |
| } | |
| extension BottomSheetTransitioningDelegate: UIViewControllerTransitioningDelegate { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BottomSheetPresentationController: UIPresentationController { | |
| private let dismissThreshold: CGFloat = 0.3 | |
| private let grabberView: UIView = { | |
| let view = UIView() | |
| view.frame.size = CGSize(width: 50, height: 3) | |
| view.backgroundColor = .lightGray | |
| return view | |
| }() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override var frameOfPresentedViewInContainerView: CGRect { | |
| guard let containerView, let presentedView else { | |
| return super.frameOfPresentedViewInContainerView | |
| } | |
| let maximumWidth = containerView.bounds.width | |
| let maximumHeight = containerView.frame.height - containerView.safeAreaInsets.top - containerView.safeAreaInsets.bottom | |
| let fittingSize = CGSize(width: maximumWidth, height: UIView.layoutFittingCompressedSize.height) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func presentationTransitionWillBegin() { | |
| super.presentationTransitionWillBegin() | |
| containerView?.addSubview(dimmedBackgroundView) | |
| dimmedBackgroundView.backgroundColor = .black.withAlphaComponent(0.5) | |
| presentedView?.addSubview(grabberView) | |
| presentedViewController.transitionCoordinator?.animate(alongsideTransition: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func containerViewDidLayoutSubviews() { | |
| super.containerViewDidLayoutSubviews() | |
| guard let presentedView, let containerView else { | |
| return | |
| } | |
| dimmedBackgroundView.frame = containerView.bounds | |
| grabberView.frame.origin.y = 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func dismissalTransitionWillBegin() { | |
| super.dismissalTransitionWillBegin() | |
| presentedViewController.transitionCoordinator?.animate(alongsideTransition: { [weak self] _ in | |
| guard let self else { return } | |
| self.presentedView?.layer.cornerRadius = .zero | |
| self.dimmedBackgroundView.backgroundColor = .clear | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension BottomSheetPresentationController { | |
| private func setupInteraction() { | |
| guard let presentedView = presentedView else { | |
| return | |
| } | |
| func addGestureIfNeeded(_ gesture: UIGestureRecognizer, to view: UIView, delegate: UIGestureRecognizerDelegate? = nil) { | |
| if let recognizers = view.gestureRecognizers, recognizers.contains(gesture) { | |
| return | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private func dismiss(withInteraction: Bool) { | |
| transitioningDelegate?.transition.wantsInteractiveStart = withInteraction | |
| presentedViewController.dismiss(animated: true) | |
| } |
OlderNewer