Skip to content

Commit

Permalink
💫 Update: Exp - AdaptiveModal
Browse files Browse the repository at this point in the history
Summary: Update experiment/test - `swift-programmatic-modal/AdaptiveModal`.
  • Loading branch information
dominicstop committed Jun 5, 2023
1 parent 85ae662 commit c579ae1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,28 @@ struct AdaptiveModalInterpolationPoint: Equatable {
// MARK: - Computed Properties
// ---------------------------

var transform: CGAffineTransform {
var transform: CGAffineTransform = .identity;
var modalTransforms: [CGAffineTransform] {
var transforms: [CGAffineTransform] = [];

transform = transform.rotated(by: self.modalRotation);
transforms.append(
.init(rotationAngle: self.modalRotation)
);

transform = transform.scaledBy(
x: self.modalScaleX,
y: self.modalScaleY
transforms.append(
.init(scaleX: self.modalScaleX, y: self.modalScaleY)
);

transform = transform.translatedBy(
x: self.modalTranslateX,
y: self.modalTranslateY
transforms.append(
.init(translationX: self.modalTranslateX, y: self.modalTranslateY)
);

return transform;
return transforms;
};

var modalTransform: CGAffineTransform {
self.modalTransforms.reduce(.identity){
$0.concatenating($1);
};
};

// MARK: - Init
Expand Down Expand Up @@ -172,38 +178,39 @@ struct AdaptiveModalInterpolationPoint: Equatable {
// MARK: - Functions
// -----------------

func getTransform(
func getModalTransform(
shouldApplyRotation: Bool = true,
shouldApplyScale: Bool = true,
shouldApplyTranslate: Bool = true
) -> CGAffineTransform {

var transform: CGAffineTransform = .identity;
var transforms: [CGAffineTransform] = [];

if shouldApplyRotation {
transform = transform.rotated(by: self.modalRotation);
transforms.append(
.init(rotationAngle: self.modalRotation)
);
};

if shouldApplyScale {
transform = transform.scaledBy(
x: self.modalScaleX,
y: self.modalScaleY
transforms.append(
.init(scaleX: self.modalScaleX, y: self.modalScaleY)
);
};

if shouldApplyTranslate {
transform = transform.translatedBy(
x: self.modalTranslateX,
y: self.modalTranslateY
transforms.append(
.init(translationX: self.modalTranslateX, y: self.modalTranslateY)
);
};

return transform;
return transforms.reduce(.identity){
$0.concatenating($1);
};
};

func apply(toModalView modalView: UIView){
modalView.frame = self.computedRect;
modalView.transform = self.transform;

modalView.layer.cornerRadius = self.modalCornerRadius;
modalView.layer.maskedCorners = self.modalMaskedCorners;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AdaptiveModalManager {

var modalConfig: AdaptiveModalConfig;

var enableSnapping = false;
var enableSnapping = true;

// MARK: - Properties - Refs
// --------------------------
Expand Down Expand Up @@ -489,52 +489,26 @@ class AdaptiveModalManager {
shouldClampMax: clampConfig.shouldClampModalLastScaleY
);

let nextTranslateX = Self.interpolate(
inputValue: inputPercentValue,
rangeInput: interpolationRangeInput,
rangeOutput: interpolationSteps.map {
$0.modalTranslateX
},
shouldClampMin: clampConfig.shouldClampModalLastTranslateX,
shouldClampMax: clampConfig.shouldClampModalLastTranslateX
);

let nextTranslateY = Self.interpolate(
inputValue: inputPercentValue,
rangeInput: interpolationRangeInput,
rangeOutput: interpolationSteps.map {
$0.modalTranslateY
},
shouldClampMin: clampConfig.shouldClampModalLastTranslateY,
shouldClampMax: clampConfig.shouldClampModalLastTranslateY
);

let nextTransform: CGAffineTransform = {
var transform: CGAffineTransform = .identity;
var transforms: [CGAffineTransform] = [];

if let rotation = nextModalRotation {
transform = transform.rotated(by: rotation);
transforms.append(
.init(rotationAngle: rotation)
);
};

if let nextScaleX = nextScaleX,
let nextScaleY = nextScaleY {

transform = transform.scaledBy(
x: nextScaleX,
y: nextScaleY
transforms.append(
.init(scaleX: nextScaleX, y: nextScaleY)
);
};

if let nextTranslateX = nextTranslateX,
let nextTranslateY = nextTranslateY {

transform = transform.translatedBy(
x: nextTranslateX,
y: nextTranslateY
);
return transforms.reduce(.identity) {
$0.concatenating($1);
};

return transform;
}();

return nextTransform;
Expand Down Expand Up @@ -849,8 +823,6 @@ class AdaptiveModalManager {
interpolationPoint.apply(toDummyModalView: self.dummyModalView);
interpolationPoint.apply(toModalBackgroundView: self.modalBackgroundView);
interpolationPoint.apply(toBackgroundView: self.backgroundDimmingView);

modalView.layoutIfNeeded();
};

if let completion = completion {
Expand Down Expand Up @@ -962,8 +934,10 @@ class AdaptiveModalManager {
};

@objc func onDisplayLinkTick(displayLink: CADisplayLink) {
guard let dummyModalViewPresentationLayer =
guard let modalView = self.modalView,
let dummyModalViewPresentationLayer =
self.dummyModalView.layer.presentation(),

let interpolationRangeMaxInput = self.interpolationRangeMaxInput
else { return };

Expand All @@ -989,7 +963,6 @@ class AdaptiveModalManager {
? Self.invertPercent(percent)
: percent;


self.applyInterpolationToBackgroundVisualEffect(
forInputPercentValue: percentAdj
);
Expand All @@ -998,6 +971,12 @@ class AdaptiveModalManager {
forInputPercentValue: percentAdj
);

if let modalTranslate = self.interpolateModalTransform(
forInputPercentValue: percentAdj
) {
modalView.transform = modalTranslate.concatenating(.identity);
};

self.prevModalFrame = nextModalFrame;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ enum AdaptiveModalConfigTestPresets: CaseIterable {
modalRotation: 0.1,
modalScaleX: 0.5,
modalScaleY: 1,
modalTranslateX: 1000,
modalTranslateY: 1000
modalTranslateX: 500,
modalTranslateY: 500
)
),
// snap point - 2
Expand All @@ -79,7 +79,7 @@ enum AdaptiveModalConfigTestPresets: CaseIterable {
modalRotation: -0.1,
modalScaleX: 1,
modalScaleY: 1,
modalTranslateX: -400,
modalTranslateX: -600,
modalTranslateY: 0
)
),
Expand Down Expand Up @@ -444,10 +444,9 @@ class RNIDraggableTestViewController : UIViewController {
};

@objc func onDragPanGestureView(_ sender: UIPanGestureRecognizer) {
print("onDragPanGestureView - velocity: \(sender.velocity(in: self.view))");
// print("onDragPanGestureView - velocity: \(sender.velocity(in: self.view))");

self.modalManager.notifyOnDragPanGesture(sender);

};
};

Expand Down

0 comments on commit c579ae1

Please sign in to comment.