Seth Willits was working on an interpolation protocol that would allow conforming constructs to interpolate from one value to another, regardless of their underlying types. It needed to work for
CGFloat as well as
Double, and be decomposable and useful for
CGRect as well as any custom Swift
This created interesting challenges, as some of the most fundamental animation curves use exponentiation, which is not built into the Swift standard library. So I turned to
pow, which is only defined for float and double:
public func powf(_: Float, _: Float) -> Float public func pow(_: Double, _: Double) -> Double
In the past, I’ve created horrible bridging solution that permits migration from most floating point values to double. It’s not beautiful but I decided to pull it out from my toolkit and introduce it to this problem.
What I did was to build an extension on
BinaryFloatingPoint that returned an interpolated value along a styled curve. My hack let me create a single
interpolate method that applied across floating-point. I bridged to
Double to use the Darwin
There’s an interesting
CGFloat bug in play here, where the value history does not display as a graph but the numbers are right and work properly during animation.
Here’s the code. Shout out your improvements and alternatives.