Skip to content

Commit

Permalink
💫 Update: Exp - swift-programmatic-modal
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed May 25, 2023
1 parent 6f1f220 commit 12a1cda
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class AdaptiveModalManager {
var targetRectProvider: () -> CGRect;
var currentSizeProvider: () -> CGSize;

var gestureOffset: CGFloat?;

weak var modalView: UIView?;

let snapPoints: [RNILayout] = [
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -154,7 +156,7 @@ class RNIDraggableTestViewController : UIViewController {
break;

case .changed:
let computedRect = self.modalManager.computeFrame(
let computedRect = self.modalManager.interpolateModalRect(
forGesturePointInTargetRect: gesturePoint,
gesturePointRelativeToModal: relativeGesturePoint
);
Expand Down

0 comments on commit 12a1cda

Please sign in to comment.