Skip to content

Cubic Animation Calculation Mode for Retaining values between Keyframes

Thisura Dodangoda edited this page May 30, 2019 · 2 revisions

In Core Animation's KeyFrame animation class you can specify the calculationMode, of how the values between each keyframe are interpolated.

Usually specifying, .cubic or .linear would suffice. However, when it is required that a value be retained over some period of time, these calculation modes could cost problems.

Assume a variable changes between 0.0 and 1.0 according to the following timings.

timings = [0.0, 0.4, 0.6, 1.0]

Accordingly, the value of the variables will be stored as follows.

animValues = [0.0, 1.0, 1.0, 0.0]

In this scenario, selecting .linear as the calculation mode will cause a robotic movement without any easing. Selecting .cubic will result in the values between the 1st and 2nd positions be interpolated to values higher than 1.0.

The solution is to modify the tension, bias and continuity values of this animation -- since according to Apple's docs, the interpolation is calculated using the Kochanek-Bartels form.

According to the wiki page at https://en.wikipedia.org/wiki/Kochanek%E2%80%93Bartels_spline, Tension, Bias and Continuity values vary between 0.0 and 1.0.

How it modifies an initial curve is as follows (extracted from the same article).

Initial Curve modification based on Tension, Bias and Curve

And to be more specific, how T, B, and C values modify the curve can be remembered as follows.

  • T -> Changes the length of the tangent vector

  • B -> Primarily changes the direction of the tangent vector

  • C -> Changes the sharpness in change between tangents

Clone this wiki locally