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 2, 2023
1 parent 767e925 commit 6ad7e7a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct AdaptiveModalInterpolationPoint: Equatable {
let modalMaskedCorners: CACornerMask;

let backgroundVisualEffect: UIVisualEffect?;
let backgroundVisualEffectIntensity: CGFloat;

init(
usingModalConfig modalConfig: AdaptiveModalConfig,
Expand Down Expand Up @@ -61,20 +62,23 @@ struct AdaptiveModalInterpolationPoint: Equatable {
};
}();


let keyframeCurrent = snapPointConfig.animationKeyframe;
let keyframePrev = prevSnapPointConfig?.animationKeyframe;

self.modalCornerRadius = keyframeCurrent?.modalCornerRadius
?? keyframePrev?.modalCornerRadius
?? Self.DefaultCornerRadius;

self.modalMaskedCorners = keyframePrev?.modalMaskedCorners
self.modalMaskedCorners = keyframeCurrent?.modalMaskedCorners
?? keyframePrev?.modalMaskedCorners
?? Self.DefaultMaskedCorners;

self.backgroundVisualEffect = keyframePrev?.backgroundVisualEffect
self.backgroundVisualEffect = keyframeCurrent?.backgroundVisualEffect
?? keyframePrev?.backgroundVisualEffect;

self.backgroundVisualEffectIntensity = keyframeCurrent?.backgroundVisualEffectIntensity
?? keyframePrev?.backgroundVisualEffectIntensity
?? 1;
};

func apply(toModalView modalView: UIView){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ extension AdaptiveModalManager {
rangeInput.count >= 2
else { return nil };

if shouldClampMin, inputValue <= rangeInput.first! {
if shouldClampMin, inputValue < rangeInput.first! {
return rangeOutput.first!;
};

if shouldClampMax, inputValue <= rangeInput.last! {
if shouldClampMax, inputValue > rangeInput.last! {
return rangeOutput.last!;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class AdaptiveModalManager {
/// `inputPercentValue` is between the range of `percentCurrent`
/// and `percentNext`
///
return percentCurrent >= inputPercentValue &&
return inputPercentValue >= percentCurrent &&
inputPercentValue <= percentNext;
};

Expand All @@ -254,8 +254,6 @@ class AdaptiveModalManager {
guard let interpolationSteps = rangeOutput ?? self.interpolationStepsSorted,
let interpolationRangeInput = rangeInput ?? self.interpolationRangeInput
else { return nil };

print("interpolationRangeInput:", interpolationRangeInput);

let clampConfig = modalConfig.interpolationClampingConfig;

Expand Down Expand Up @@ -339,32 +337,30 @@ class AdaptiveModalManager {
func applyInterpolationToBackgroundVisualEffect(
forInputPercentValue inputPercentValue: CGFloat
) {
guard let backgroundVisualEffectView = self.backgroundVisualEffectView,
let inputRange = self.getInterpolationStepRange(
forInputPercentValue: inputPercentValue
)
else { return };

let animator: AdaptiveModalPropertyAnimator = {
let animator: AdaptiveModalPropertyAnimator? = {
if let animator = self.backgroundVisualEffectAnimator {
return animator;
};

guard let backgroundVisualEffectView = self.backgroundVisualEffectView,
let inputRange = self.getInterpolationStepRange(
forInputPercentValue: inputPercentValue
)
else { return nil };

return AdaptiveModalPropertyAnimator(
interpolationRangeStart: inputRange.rangeStart,
interpolationRangeEnd: inputRange.rangeEnd,
forComponent: backgroundVisualEffectView,
withInputAxisKey: self.modalConfig.inputValueKeyForPoint
interpolationOutputKey: \.backgroundVisualEffectIntensity
) {
$0.effect = $1.backgroundVisualEffect;
};
}();

print(
"applyInterpolationToBackgroundVisualEffect - inputPercentValue: \(inputPercentValue)"
);

guard let animator = animator else { return };
self.backgroundVisualEffectAnimator = animator;

animator.setFractionComplete(forInputPercentValue: inputPercentValue);
};

Expand Down Expand Up @@ -413,14 +409,6 @@ class AdaptiveModalManager {
let percentAdj = shouldInvertPercent
? Self.invertPercent(percent)
: percent;

print(
"applyInterpolationToModal"
+ " - inputValue: \(inputValue)"
+ " - maxInputValue: \(interpolationRangeMaxInput)"
+ " - percent: \(percent)"
+ " - percentAdj: \(percentAdj)"
);

self.applyInterpolationToModal(forInputPercentValue: percentAdj);
};
Expand All @@ -447,14 +435,6 @@ class AdaptiveModalManager {
x: gesturePoint.x - gestureOffset.x,
y: gesturePoint.y - gestureOffset.y
);

print(
"applyInterpolationToModal"
+ " - gesturePoint: \(gesturePoint)"
+ " - gestureOffset: \(gestureOffset)"
+ " - gestureInputPoint: \(gestureInputPoint)"
+ " - modalRect: \(modalRect.origin)"
);

self.applyInterpolationToModal(forPoint: gestureInputPoint);
};
Expand Down Expand Up @@ -657,11 +637,6 @@ class AdaptiveModalManager {
forInputPercentValue: percentAdj
);

print(
"onDisplayLinkTick"
+ " - percentAdj: \(percentAdj)"
);

self.prevModalFrame = nextModalFrame;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,27 @@
import UIKit

struct AdaptiveModalPropertyAnimator {

var inputAxisKey: KeyPath<CGPoint, CGFloat>;

var interpolationRangeStart: AdaptiveModalInterpolationPoint;
var interpolationRangeEnd: AdaptiveModalInterpolationPoint;

let interpolationOutputKey:
KeyPath<AdaptiveModalInterpolationPoint, CGFloat>;

var animator: UIViewPropertyAnimator;

private weak var component: AnyObject?;

private var inputRangeStart: CGFloat {
self.interpolationRangeStart.percent;
};

private var inputRangeEnd: CGFloat {
self.interpolationRangeEnd.percent;
};
private var range: [AdaptiveModalInterpolationPoint] {[
self.interpolationRangeStart,
self.interpolationRangeEnd
]};

init<T: AnyObject>(
interpolationRangeStart: AdaptiveModalInterpolationPoint,
interpolationRangeEnd: AdaptiveModalInterpolationPoint,
forComponent component: T,
withInputAxisKey inputAxisKey: KeyPath<CGPoint, CGFloat>,
interpolationOutputKey: KeyPath<AdaptiveModalInterpolationPoint, CGFloat>,
animation: @escaping (
_ component: T,
_ interpolationPoint: AdaptiveModalInterpolationPoint
Expand All @@ -39,7 +37,7 @@ struct AdaptiveModalPropertyAnimator {
self.interpolationRangeStart = interpolationRangeStart;
self.interpolationRangeEnd = interpolationRangeEnd;

self.inputAxisKey = inputAxisKey;
self.interpolationOutputKey = interpolationOutputKey;
self.component = component;

let animator = UIViewPropertyAnimator(
Expand Down Expand Up @@ -87,10 +85,17 @@ struct AdaptiveModalPropertyAnimator {
func setFractionComplete(
forInputPercentValue inputPercentValue: CGFloat
) {
let inputRangeEndAdj = self.inputRangeEnd - self.inputRangeStart;
let inputPercentAdj = inputPercentValue - self.inputRangeStart;
let percent = AdaptiveModalManager.interpolate(
inputValue: inputPercentValue,
rangeInput: range.map {
$0.percent
},
rangeOutput: range.map {
$0[keyPath: self.interpolationOutputKey]
}
);

let percent = inputPercentAdj / inputRangeEndAdj;
guard let percent = percent else { return };
self.setFractionComplete(forPercent: percent);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ enum AdaptiveModalConfigTestPresets: CaseIterable {
modalMaskedCorners: [
.layerMinXMinYCorner,
.layerMaxXMinYCorner
]
],
backgroundVisualEffect: UIBlurEffect(style: .regular),
backgroundVisualEffectIntensity: 0
)
),
AdaptiveModalSnapPointConfig(
Expand All @@ -100,7 +102,8 @@ enum AdaptiveModalConfigTestPresets: CaseIterable {
.layerMaxXMinYCorner,
.layerMaxXMaxYCorner
],
backgroundVisualEffect: UIBlurEffect(style: .regular)
backgroundVisualEffect: UIBlurEffect(style: .regular),
backgroundVisualEffectIntensity: 0.5
)
),
],
Expand Down

0 comments on commit 6ad7e7a

Please sign in to comment.