Created
February 17, 2017 21:48
-
-
Save arronhunt/805c18b2aef2076aafd56d9113d33d67 to your computer and use it in GitHub Desktop.
Modulation function in Swift
This file contains hidden or 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
// let value = Modulate(1, from: [0, 10], to: [0, 20], true); | |
// print(value); | |
// Returns 2 | |
func Modulate(_ input: Float, from: [Float], to: [Float], limit: Bool) -> Float { | |
let result = to[0] + (((input - from[0]) / (from[1] - from[0])) * (to[1] - to[0])); | |
if (limit) { | |
if (to[0] < to[1]) { | |
if (result < to[0]) { return to[0] } | |
if (result > to[1]) { return to[1] } | |
} | |
else { | |
if (result > to[0]) { return to[0] } | |
if (result < to[1]) { return to[1] } | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I noticed this is actually the same way how FramerJS does.
But this can be more simplified in Swift.
To deal with UIView animations like
CGAffineTransform
better use CGFloat instead.