From 6e436b291eeb78524e86f009340318cf019580fe Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Sat, 10 Jun 2023 16:08:36 +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`. --- ...IViewControllerTransitioningDelegate.swift | 4 +- .../AdaptiveModal/AdaptiveModalManager.swift | 100 +++++++++++------- ...eModalPresentationTestViewController.swift | 7 ++ .../Test/RNIDraggableTestViewController.swift | 26 ++--- 4 files changed, 83 insertions(+), 54 deletions(-) diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift index 3ee54f2a..42cd0f6a 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+UIViewControllerTransitioningDelegate.swift @@ -15,13 +15,13 @@ extension AdaptiveModalManager: UIViewControllerTransitioningDelegate { presenting: UIViewController, source: UIViewController ) -> UIViewControllerAnimatedTransitioning? { - return nil; + return self; }; func animationController( forDismissed dismissed: UIViewController ) -> UIViewControllerAnimatedTransitioning? { - return nil; + return self; }; func presentationController( diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift index b124db8f..9940cab2 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift @@ -230,8 +230,13 @@ class AdaptiveModalManager: NSObject { super.init(); self.computeSnapPoints(); + self.setupInitViews(); self.setupDummyModalView(); + self.setupGestureHandler(); + + self.setupAddViews(); + self.setupViewConstraints(); }; init(modalConfig: AdaptiveModalConfig) { @@ -265,6 +270,17 @@ class AdaptiveModalManager: NSObject { self.backgroundVisualEffectView = UIVisualEffectView(); }; + func setupGestureHandler(){ + guard let modalView = self.modalView else { return }; + + modalView.addGestureRecognizer( + UIPanGestureRecognizer( + target: self, + action: #selector(self.onDragPanGesture(_:)) + ) + ); + }; + private func setupDummyModalView(){ guard let targetView = self.targetView else { return }; let dummyModalView = self.dummyModalView; @@ -276,7 +292,7 @@ class AdaptiveModalManager: NSObject { targetView.addSubview(dummyModalView); }; - func setupAddViews(){ + private func setupAddViews(){ guard let modalView = self.modalView, let targetView = self.targetView else { return }; @@ -1026,6 +1042,44 @@ class AdaptiveModalManager: NSObject { return nextIndex; }; + @objc func onDragPanGesture(_ sender: UIPanGestureRecognizer) { + let gesturePoint = sender.location(in: self.targetView); + self.gesturePoint = gesturePoint; + + let gestureVelocity = sender.velocity(in: self.targetView); + self.gestureVelocity = gestureVelocity; + + switch sender.state { + case .began: + self.gestureInitialPoint = gesturePoint; + + case .changed: + self.modalAnimator?.stopAnimation(true); + + self.applyInterpolationToModal(forGesturePoint: gesturePoint); + self.onModalWillSnap(); + + case .cancelled, .ended: + guard self.enableSnapping else { + self.clearGestureValues(); + return; + }; + + let gestureFinalPoint = self.gestureFinalPoint ?? gesturePoint; + + self.snapToClosestSnapPoint(forPoint: gestureFinalPoint) { + self.endDisplayLink(); + self.onModalDidSnap(); + }; + + self.clearGestureValues(); + self.startDisplayLink(); + + default: + break; + }; + }; + // MARK: - Functions - DisplayLink-Related // --------------------------------------- @@ -1103,7 +1157,13 @@ class AdaptiveModalManager: NSObject { self.setupViewControllers(); self.setupDummyModalView(); self.setupInitViews(); + self.setupGestureHandler(); + + self.setupAddViews(); self.setupViewConstraints(); + + self.computeSnapPoints(); + self.updateModal(); }; func computeSnapPoints( @@ -1117,44 +1177,6 @@ class AdaptiveModalManager: NSObject { ); }; - func notifyOnDragPanGesture(_ gesture: UIPanGestureRecognizer){ - let gesturePoint = gesture.location(in: self.targetView); - self.gesturePoint = gesturePoint; - - let gestureVelocity = gesture.velocity(in: self.targetView); - self.gestureVelocity = gestureVelocity; - - switch gesture.state { - case .began: - self.gestureInitialPoint = gesturePoint; - - case .changed: - self.modalAnimator?.stopAnimation(true); - - self.applyInterpolationToModal(forGesturePoint: gesturePoint); - self.onModalWillSnap(); - - case .cancelled, .ended: - guard self.enableSnapping else { - self.clearGestureValues(); - return; - }; - - let gestureFinalPoint = self.gestureFinalPoint ?? gesturePoint; - - self.snapToClosestSnapPoint(forPoint: gestureFinalPoint) { - self.endDisplayLink(); - self.onModalDidSnap(); - }; - - self.clearGestureValues(); - self.startDisplayLink(); - - default: - break; - }; - }; - func updateModal(){ guard !self.isAnimating else { return }; diff --git a/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift b/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift index 69de3c3e..2fce756d 100644 --- a/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift +++ b/experiments/swift-programmatic-modal/Test/AdaptiveModalPresentationTestViewController.swift @@ -69,6 +69,13 @@ class AdaptiveModalPresentationTestViewController : UIViewController { @objc func onPressButtonPresentViewController(_ sender: UIButton) { let testVC = TestViewController(); + + self.adaptiveModalManager.set( + viewControllerToPresent: testVC, + presentingViewController: self + ); + + self.present(testVC, animated: true); }; }; diff --git a/experiments/swift-programmatic-modal/Test/RNIDraggableTestViewController.swift b/experiments/swift-programmatic-modal/Test/RNIDraggableTestViewController.swift index 306fa515..ab6dfdf9 100644 --- a/experiments/swift-programmatic-modal/Test/RNIDraggableTestViewController.swift +++ b/experiments/swift-programmatic-modal/Test/RNIDraggableTestViewController.swift @@ -369,12 +369,12 @@ class RNIDraggableTestViewController : UIViewController { // alpha: 0 // ); - view.addGestureRecognizer( - UIPanGestureRecognizer( - target: self, - action: #selector(self.onDragPanGestureView(_:)) - ) - ); + // view.addGestureRecognizer( + // UIPanGestureRecognizer( + // target: self, + // action: #selector(self.onDragPanGestureView(_:)) + // ) + // ); let floatingViewLabel = self.floatingViewLabel; view.addSubview(floatingViewLabel); @@ -435,8 +435,8 @@ class RNIDraggableTestViewController : UIViewController { dummyBackgroundView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor), ]); - self.modalManager.setupAddViews(); - self.modalManager.setupViewConstraints(); + // self.modalManager.setupAddViews(); + // self.modalManager.setupViewConstraints(); }; override func viewDidLayoutSubviews() { @@ -449,11 +449,11 @@ class RNIDraggableTestViewController : UIViewController { // self.updateFloatingView(isAnimated: true); }; - @objc func onDragPanGestureView(_ sender: UIPanGestureRecognizer) { - // print("onDragPanGestureView - velocity: \(sender.velocity(in: self.view))"); - - self.modalManager.notifyOnDragPanGesture(sender); - }; + // @objc func onDragPanGestureView(_ sender: UIPanGestureRecognizer) { + // // print("onDragPanGestureView - velocity: \(sender.velocity(in: self.view))"); + // + // self.modalManager.notifyOnDragPanGesture(sender); + // }; }; extension RNIDraggableTestViewController: AdaptiveModalEventNotifiable {