-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Update experiment/test - `swift-programmatic-modal/AdaptiveModal`.
- Loading branch information
1 parent
e9e90f5
commit 9932f39
Showing
6 changed files
with
172 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
experiments/swift-programmatic-modal/AdaptiveModal/AdaptiveModalManager+Helpers.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// AdaptiveModalManager+Helpers.swift | ||
// swift-programmatic-modal | ||
// | ||
// Created by Dominic Go on 5/27/23. | ||
// | ||
|
||
import Foundation | ||
|
||
extension AdaptiveModalManager { | ||
|
||
static func interpolate( | ||
inputValue : CGFloat, | ||
rangeInput : [CGFloat], | ||
rangeOutput: [CGFloat] | ||
) -> CGFloat? { | ||
|
||
guard rangeInput.count == rangeOutput.count, | ||
rangeInput.count >= 2 | ||
else { return nil }; | ||
|
||
// A - Extrapolate Left | ||
if inputValue < rangeInput.first! { | ||
|
||
let rangeInputStart = rangeInput.first!; | ||
let rangeOutputStart = rangeOutput.first!; | ||
|
||
let delta1 = rangeInputStart - inputValue; | ||
let percent = delta1 / rangeInputStart; | ||
|
||
// extrapolated "range output end" | ||
let rangeOutputEnd = rangeOutputStart - (rangeOutput[1] - rangeOutputStart); | ||
|
||
let interpolatedValue = RNIAnimator.EasingFunctions.lerp( | ||
valueStart: rangeOutputEnd, | ||
valueEnd: rangeOutputStart, | ||
percent: percent | ||
); | ||
|
||
let delta2 = interpolatedValue - rangeOutputEnd; | ||
return rangeOutputStart - delta2; | ||
}; | ||
|
||
let (rangeStartIndex, rangeEndIndex): (Int, Int) = { | ||
let rangeInputEnumerated = rangeInput.enumerated(); | ||
|
||
let match = rangeInputEnumerated.first { | ||
guard let nextValue = rangeInput[safeIndex: $0.offset + 1] | ||
else { return false }; | ||
|
||
return inputValue >= $0.element && inputValue < nextValue; | ||
}; | ||
|
||
// B - Interpolate Between | ||
if let match = match { | ||
let rangeStartIndex = match.offset; | ||
return (rangeStartIndex, rangeStartIndex + 1); | ||
}; | ||
|
||
let lastIndex = rangeInput.count - 1; | ||
let secondToLastIndex = rangeInput.count - 2; | ||
|
||
// C - Extrapolate Right | ||
return (secondToLastIndex, lastIndex); | ||
}(); | ||
|
||
guard let rangeInputStart = rangeInput [safeIndex: rangeStartIndex], | ||
let rangeInputEnd = rangeInput [safeIndex: rangeEndIndex ], | ||
let rangeOutputStart = rangeOutput[safeIndex: rangeStartIndex], | ||
let rangeOutputEnd = rangeOutput[safeIndex: rangeEndIndex ] | ||
else { return nil }; | ||
|
||
let inputValueAdj = inputValue - rangeInputStart; | ||
let rangeInputEndAdj = rangeInputEnd - rangeInputStart; | ||
|
||
let progress = inputValueAdj / rangeInputEndAdj; | ||
|
||
return RNIAnimator.EasingFunctions.lerp( | ||
valueStart: rangeOutputStart, | ||
valueEnd : rangeOutputEnd, | ||
percent : progress | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.