From ef5f737c560083c9d41048163ae22a6cefd03e38 Mon Sep 17 00:00:00 2001 From: Dominic Go <18517029+dominicstop@users.noreply.github.com> Date: Sun, 18 Jun 2023 06:28:40 +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`. --- .../AdaptiveModal/AdaptiveModalConfig.swift | 9 +++ .../AdaptiveModal/AdaptiveModalManager.swift | 76 ++++++++++++++----- .../RNILayout/RNILayoutValueMode.swift | 2 +- 3 files changed, 69 insertions(+), 18 deletions(-) diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalConfig.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalConfig.swift index 2dbcae6d..0bbe115f 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalConfig.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalConfig.swift @@ -64,6 +64,15 @@ struct AdaptiveModalConfig { }; }; + var inputValueKeyForRect: KeyPath { + switch self.snapDirection { + case .bottomToTop: return \.minY; + case .topToBottom: return \.maxY; + case .leftToRight: return \.maxX; + case .rightToLeft: return \.minX; + }; + }; + var maxInputRangeKeyForRect: KeyPath { switch self.snapDirection { case .bottomToTop, .topToBottom: return \.height; diff --git a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift index c56267ec..8beb1676 100644 --- a/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift +++ b/experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager.swift @@ -200,6 +200,41 @@ class AdaptiveModalManager: NSObject { return CGPoint(x: nextX, y: nextY); }; + private var computedGestureOffset: CGPoint? { + guard let gestureInitialPoint = self.gestureInitialPoint, + let modalRect = self.modalFrame + else { return nil }; + + if let gestureOffset = self.gestureOffset { + return gestureOffset; + }; + + let xOffset: CGFloat = { + switch self.modalConfig.snapDirection { + case .bottomToTop, .rightToLeft: + return gestureInitialPoint.x - modalRect.minX; + + case .topToBottom, .leftToRight: + return modalRect.maxX - gestureInitialPoint.x; + }; + }(); + + let yOffset: CGFloat = { + switch self.modalConfig.snapDirection { + case .bottomToTop, .rightToLeft: + return gestureInitialPoint.y - modalRect.minY; + + case .topToBottom, .leftToRight: + return modalRect.maxY - gestureInitialPoint.y; + }; + }(); + + let offset = CGPoint(x: xOffset, y: yOffset); + self.gestureOffset = offset; + + return offset; + }; + // MARK: - Properties // ------------------- @@ -945,31 +980,38 @@ class AdaptiveModalManager: NSObject { let percentAdj = shouldInvertPercent ? AdaptiveModalUtilities.invertPercent(percent) : percent; + + print( + "applyInterpolationToModal" + + "\n - inputValue: \(inputValue)" + + "\n - interpolationRangeMaxInput: \(interpolationRangeMaxInput)" + + "\n - percent: \(percent)" + + "\n - interpolationRangeInput: \(self.interpolationRangeInput!)" + + "\n" + ); self.applyInterpolationToModal(forInputPercentValue: percentAdj); }; private func applyInterpolationToModal(forGesturePoint gesturePoint: CGPoint) { - guard let gestureInitialPoint = self.gestureInitialPoint + guard let computedGestureOffset = self.computedGestureOffset else { return }; - let modalRect = self.modalFrame!; - - let gestureOffset = self.gestureOffset ?? { - return CGPoint( - x: gestureInitialPoint.x - modalRect.origin.x, - y: gestureInitialPoint.y - modalRect.origin.y - ); + let gestureInputPoint: CGPoint = { + switch self.modalConfig.snapDirection { + case .bottomToTop, .rightToLeft: + return CGPoint( + x: gesturePoint.x - computedGestureOffset.x, + y: gesturePoint.y - computedGestureOffset.y + ); + + case .topToBottom, .leftToRight: + return CGPoint( + x: gesturePoint.x + computedGestureOffset.x, + y: gesturePoint.y + computedGestureOffset.y + ); + }; }(); - - if self.gestureOffset == nil { - self.gestureOffset = gestureOffset; - }; - - let gestureInputPoint = CGPoint( - x: gesturePoint.x - gestureOffset.x, - y: gesturePoint.y - gestureOffset.y - ); self.applyInterpolationToModal(forPoint: gestureInputPoint); }; diff --git a/experiments/swift-programmatic-modal/RNILayout/RNILayoutValueMode.swift b/experiments/swift-programmatic-modal/RNILayout/RNILayoutValueMode.swift index c70ce312..6dacc6ca 100644 --- a/experiments/swift-programmatic-modal/RNILayout/RNILayoutValueMode.swift +++ b/experiments/swift-programmatic-modal/RNILayout/RNILayoutValueMode.swift @@ -22,7 +22,7 @@ public enum RNILayoutValueMode { insetKey: KeyPath ); - case multipleValues(_ values: [RNILayoutValueMode]); + case multipleValues(_ values: [Self]); // MARK: Functions // ---------------