From 12a1cda0642c4a45e3fe65460adb293487a1138c Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Fri, 26 May 2023 04:12:32 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AB=20Update:=20Exp=20-=20`swift-progr?= =?UTF-8?q?ammatic-modal`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AdaptiveModal/AdaptiveModalManager.swift | 84 ++++++++++++++++++- .../Test/RNIDraggableTestViewController.swift | 4 +- 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift index 6a55f9a4..4c243deb 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift @@ -86,6 +86,8 @@ class AdaptiveModalManager { var targetRectProvider: () -> CGRect; var currentSizeProvider: () -> CGSize; + var gestureOffset: CGFloat?; + weak var modalView: UIView?; let snapPoints: [RNILayout] = [ @@ -202,7 +204,87 @@ class AdaptiveModalManager { ); }; - func computeFrame( + func interpolateModalRect( + forGesturePointInTargetRect gesturePointInTargetRect: CGPoint, + gesturePointRelativeToModal: CGPoint + ) -> CGRect { + guard let modalView = self.modalView else { return .zero }; + + let targetRect = self.targetRectProvider(); + let modalRect = modalView.frame; + + let snapRects = self.computedSnapRects.reversed(); + + let gestureOffset = self.gestureOffset ?? + gesturePointInTargetRect.y - modalRect.origin.y; + + if self.gestureOffset == nil { + self.gestureOffset = gestureOffset; + }; + + let gestureInput = gesturePointInTargetRect.y - gestureOffset; + let rangeInputGesture = snapRects.map { $0.minY }; + + print( + "gesturePoint: \(gesturePointInTargetRect)" + + "\n" + " - gesturePointRelativeToModal: \(gesturePointRelativeToModal)" + + "\n" + " - targetRect: \(targetRect)" + + "\n" + " - gestureInput: \(gestureInput)" + + "\n" + " - offset: \(gestureOffset)" + + "\n" + " - snapRects: \(snapRects)" + + "\n" + " - rangeInputGesture: \(rangeInputGesture)" + ); + + let nextHeight = Self.interpolate( + inputValue: gestureInput, + rangeInput: rangeInputGesture, + rangeOutput: snapRects.map { $0.height } + ); + + print(" - nextHeight: \(nextHeight!)"); + + let nextWidth = Self.interpolate( + inputValue: gestureInput, + rangeInput: rangeInputGesture, + rangeOutput: snapRects.map { $0.width } + ); + + print(" - nextWidth: \(nextWidth!)"); + + let nextX = Self.interpolate( + inputValue: gestureInput, + rangeInput: rangeInputGesture, + rangeOutput: snapRects.map { $0.minX } + ); + + print(" - nextX: \(nextX!)"); + + let nextY = Self.interpolate( + inputValue: gestureInput, + rangeInput: rangeInputGesture, + rangeOutput: snapRects.map { $0.minY } + )!; + + print(" - nextY: \(nextY)"); + + let nextRect = CGRect( + x: nextX!, + y: nextY, + width: nextWidth!, + height: nextHeight! + ); + + print( + " - modalRect: \(modalRect)" + + "\n" + " - nextRect: \(nextRect)" + + "\n" + ); + + return nextRect; + }; + + + func _computeFrame( forGesturePointInTargetRect gesturePointInTargetRect: CGPoint, gesturePointRelativeToModal: CGPoint ) -> CGRect { diff --git a/experiments/swift-programmatic-modal/Test/RNIDraggableTestViewController.swift b/experiments/swift-programmatic-modal/Test/RNIDraggableTestViewController.swift index d1c40d6c..46167071 100644 --- a/experiments/swift-programmatic-modal/Test/RNIDraggableTestViewController.swift +++ b/experiments/swift-programmatic-modal/Test/RNIDraggableTestViewController.swift @@ -139,6 +139,8 @@ class RNIDraggableTestViewController : UIViewController { self.initialGesturePoint = gesturePoint; case .cancelled, .ended: + self.modalManager.gestureOffset = nil; + let gesturePointDiff = self.initialGesturePoint.y - gesturePoint.y; let isIncreasing = gesturePointDiff >= 0; @@ -154,7 +156,7 @@ class RNIDraggableTestViewController : UIViewController { break; case .changed: - let computedRect = self.modalManager.computeFrame( + let computedRect = self.modalManager.interpolateModalRect( forGesturePointInTargetRect: gesturePoint, gesturePointRelativeToModal: relativeGesturePoint );