From 317e20db1f8cd85fe58072a2278e1f362973519d Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Fri, 16 Jun 2023 22:44:13 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AB=20Update:=20Exp=20-=20`AdaptiveMod?= =?UTF-8?q?al`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Update experiment/test - `swift-programmatic-modal/AdaptiveModal`. --- ...IViewControllerAnimatedTransitioning.swift | 38 ++++++++- ...IViewControllerTransitioningDelegate.swift | 53 +++++++++---- .../AdaptiveModal/AdaptiveModalManager.swift | 77 +++++++++++-------- .../AdaptiveModalPresentationController.swift | 25 ++++-- ...eModalPresentationTestViewController.swift | 8 +- .../Test/TestRoutes.swift | 2 +- 6 files changed, 147 insertions(+), 56 deletions(-) diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift index b689c435..5fa8b60c 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerAnimatedTransitioning.swift @@ -12,13 +12,45 @@ extension AdaptiveModalManager: UIViewControllerAnimatedTransitioning { func transitionDuration( using transitionContext: UIViewControllerContextTransitioning? ) -> TimeInterval { - // to be implemented - return 1; + + return self.modalConfig.snapAnimationConfig.springAnimationSettlingTime; }; func animateTransition( using transitionContext: UIViewControllerContextTransitioning ) { - //TBA + guard let fromVC = transitionContext.viewController(forKey: .from), + let toVC = transitionContext.viewController(forKey: .to) + else { return } + + self.targetView = transitionContext.containerView; + self.targetViewController = fromVC; + + self.prepareForPresentation(); + self.showModal() { + transitionContext.completeTransition(true); + }; + + self.debug(prefix: "AdaptiveModalPresentationController.animateTransition"); + + + + + // if self.currentInterpolationIndex == 0 { + // self.prepareForPresentation( + // presentingViewController: fromVC, + // presentedViewController: toVC + // ); + // }; + // + // self.showModal(){ + // transitionContext.completeTransition(true); + // }; + + print( + "AdaptiveModalPresentationController.animateTransition" + + "\n - fromVC: \(fromVC)" + + "\n - toVC: \(toVC)" + ); }; }; diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift index a9c1ce0c..4b95513f 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift @@ -7,21 +7,18 @@ import UIKit -extension AdaptiveModalManager: UIViewControllerTransitioningDelegate { - - func animationController( - forPresented presented: UIViewController, - presenting: UIViewController, - source: UIViewController - ) -> UIViewControllerAnimatedTransitioning? { - return self; +extension AdaptiveModalManager: UIAdaptivePresentationControllerDelegate { + func adaptivePresentationStyle( + for controller: UIPresentationController, + traitCollection: UITraitCollection + ) -> UIModalPresentationStyle { + + return .custom; }; +}; - func animationController( - forDismissed dismissed: UIViewController - ) -> UIViewControllerAnimatedTransitioning? { - return self; - }; + +extension AdaptiveModalManager: UIViewControllerTransitioningDelegate { func presentationController( forPresented presented: UIViewController, @@ -35,7 +32,35 @@ extension AdaptiveModalManager: UIViewControllerTransitioningDelegate { modalManager: self ); - //presentationController.delegate = self; + print( + "AdaptiveModalManager+UIViewControllerTransitioningDelegate" + + "\n - presentationController" + + "\n - presented: \(presented)" + + "\n - presented.view.frame: \(presented.view.frame)" + + "\n - presenting: \(presenting)" + + "\n - presenting.view.frame: \(presenting?.view.frame)" + + "\n - source: \(source)" + + "\n - source.view.frame: \(source.view.frame)" + + "\n" + ); + + presentationController.delegate = self; return presentationController; }; + + func animationController( + forPresented presented: UIViewController, + presenting: UIViewController, + source: UIViewController + ) -> UIViewControllerAnimatedTransitioning? { + + return self; + }; + + func animationController( + forDismissed dismissed: UIViewController + ) -> UIViewControllerAnimatedTransitioning? { + + return self; + }; }; diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift index 0072816b..6c82d6cd 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift @@ -232,14 +232,14 @@ class AdaptiveModalManager: NSObject { // MARK: - Functions - Setup // ------------------------- - private func setupViewControllers() { + func setupViewControllers() { guard let modalVC = self.modalViewController else { return }; modalVC.modalPresentationStyle = .custom; modalVC.transitioningDelegate = self; }; - private func setupInitViews() { + func setupInitViews() { self.modalBackgroundView = UIView(); self.modalBackgroundVisualEffectView = UIVisualEffectView(); @@ -247,7 +247,7 @@ class AdaptiveModalManager: NSObject { self.backgroundVisualEffectView = UIVisualEffectView(); }; - private func setupGestureHandler() { + func setupGestureHandler() { guard let modalView = self.modalView else { return }; modalView.gestureRecognizers?.removeAll(); @@ -260,7 +260,7 @@ class AdaptiveModalManager: NSObject { ); }; - private func setupDummyModalView() { + func setupDummyModalView() { guard let targetView = self.targetView else { return }; let dummyModalView = self.dummyModalView; @@ -274,7 +274,7 @@ class AdaptiveModalManager: NSObject { targetView.addSubview(dummyModalView); }; - private func setupAddViews() { + func setupAddViews() { guard let modalView = self.modalView, let targetView = self.targetView else { return }; @@ -320,7 +320,7 @@ class AdaptiveModalManager: NSObject { }; }; - private func setupViewConstraints() { + func setupViewConstraints() { guard let modalView = self.modalView, let targetView = self.targetView else { return }; @@ -906,13 +906,6 @@ class AdaptiveModalManager: NSObject { self.clearGestureValues(); self.clearAnimators(); self.cleanupViews(); - - print( - "\n - self.backgroundVisualEffectView: \(self.backgroundVisualEffectView)", - "\n - self.backgroundDimmingView: \(self.backgroundDimmingView)", - "\n - self.modalBackgroundVisualEffectView: \(self.modalBackgroundVisualEffectView)", - "\n - self.modalBackgroundView: \(self.modalBackgroundView)" - ); }; // MARK: - Functions @@ -1127,6 +1120,23 @@ class AdaptiveModalManager: NSObject { }; }; + func debug(prefix: String? = ""){ + print( + "\n - AdaptiveModalManager.debug - \(prefix ?? "N/A")" + + "\n - modalView: \(self.modalView?.debugDescription ?? "N/A")" + + "\n - modalView frame: \(self.modalView?.frame.debugDescription ?? "N/A")" + + "\n - modalView superview: \(self.modalView?.superview.debugDescription ?? "N/A")" + + "\n - targetView: \(self.targetView?.debugDescription ?? "N/A")" + + "\n - targetView frame: \(self.targetView?.frame.debugDescription ?? "N/A")" + + "\n - targetView superview: \(self.targetView?.superview.debugDescription ?? "N/A")" + + "\n - modalViewController: \(self.modalViewController?.debugDescription ?? "N/A")" + + "\n - targetViewController: \(self.targetViewController?.debugDescription ?? "N/A")" + + "\n - currentInterpolationIndex: \(self.currentInterpolationIndex)" + + "\n - modalView gestureRecognizers: \(self.modalView?.gestureRecognizers.debugDescription ?? "N/A")" + + "\n" + ); + }; + // MARK: - Functions - DisplayLink-Related // --------------------------------------- @@ -1250,34 +1260,39 @@ class AdaptiveModalManager: NSObject { // ------------------------------ func prepareForPresentation( - modalView: UIView, - targetView: UIView + modalView: UIView? = nil, + targetView: UIView? = nil ) { - let didViewsChange = - modalView !== self.modalView || targetView !== self.targetView; - - if didViewsChange { - self.cleanup(); - }; + guard let modalView = modalView ?? self.modalView, + let targetView = targetView ?? self.targetView + else { return }; + + self.cleanup(); self.modalView = modalView; self.targetView = targetView; self.computeSnapPoints(); - self.setupViewControllers(); - if didViewsChange { - self.setupInitViews(); - self.setupDummyModalView(); - self.setupGestureHandler(); - - self.setupAddViews(); - self.setupViewConstraints(); - }; - + self.setupInitViews(); + self.setupDummyModalView(); + self.setupGestureHandler(); + + self.setupAddViews(); + self.setupViewConstraints(); + self.updateModal(); }; + func prepareForPresentation( + presentingViewController presentingVC: UIViewController + ) { + self.modalViewController = presentingVC; + self.modalView = presentingVC.view; + + self.setupViewControllers(); + }; + func notifyDidLayoutSubviews() { self.computeSnapPoints(); self.updateModal(); diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalPresentationController.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalPresentationController.swift index e28dc29c..f1ed2067 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalPresentationController.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalPresentationController.swift @@ -24,11 +24,26 @@ class AdaptiveModalPresentationController: UIPresentationController { self.modalManager = modalManager; }; - override func presentationTransitionWillBegin() { + override func presentationTransitionWillBegin() { + print( + "AdaptiveModalPresentationController.presentationTransitionWillBegin" + + "\n - presentedViewController: \(self.presentedViewController)" + + "\n - presenting: \(self.presentingViewController)" + + "\n - presentedViewController - frame: \(self.presentedViewController.view.frame)" + + "\n - presenting - frame: \(self.presentingViewController.view.frame)" + + "\n - presentedViewController - superview: \(self.presentedViewController.view.superview)" + + "\n - presenting - superview: \(self.presentingViewController.view.superview)" + + "\n" + ); - }; + }; - override func presentationTransitionDidEnd(_ completed: Bool) { - - }; + override func presentationTransitionDidEnd(_ completed: Bool) { + print( + "AdaptiveModalPresentationController.presentationTransitionDidEnd" + + "\n - presentedViewController: \(self.presentedViewController)" + + "\n - presenting: \(self.presentingViewController)" + + "\n" + ); + }; }; diff --git a/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift b/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift index 71841197..6a9ddc38 100644 --- a/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift +++ b/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift @@ -7,7 +7,7 @@ import UIKit -class TestViewController: UIViewController { +class TestModalViewController: UIViewController { override func viewDidLoad() { self.view.backgroundColor = .white; }; @@ -68,7 +68,11 @@ class AdaptiveModalPresentationTestViewController : UIViewController { }; @objc func onPressButtonPresentViewController(_ sender: UIButton) { - let testVC = TestViewController(); + let testVC = TestModalViewController(); + + self.adaptiveModalManager.prepareForPresentation( + presentingViewController: testVC + ); self.present(testVC, animated: true); }; diff --git a/experiments/swift-programmatic-modal/Test/TestRoutes.swift b/experiments/swift-programmatic-modal/Test/TestRoutes.swift index 1911729f..f6ae2d41 100644 --- a/experiments/swift-programmatic-modal/Test/TestRoutes.swift +++ b/experiments/swift-programmatic-modal/Test/TestRoutes.swift @@ -8,7 +8,7 @@ import UIKit enum TestRoutes { - static let rootRouteKey: Self = .RNIDraggableTest; + static let rootRouteKey: Self = .AdaptiveModalPresentationTest; case RNILayoutTest; case RNIDraggableTest;