Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found this:</p> <p><a href="http://cocoawithlove.com/2008/09/parametric-acceleration-curves-in-core.html">Cocoa with Love - Parametric acceleration curves in Core Animation</a></p> <p>But I think it can be made a little simpler and more readable by using blocks. So we can define a category on CAKeyframeAnimation that looks something like this:</p> <p>CAKeyframeAnimation+Parametric.h:</p> <pre><code>// this should be a function that takes a time value between // 0.0 and 1.0 (where 0.0 is the beginning of the animation // and 1.0 is the end) and returns a scale factor where 0.0 // would produce the starting value and 1.0 would produce the // ending value typedef double (^KeyframeParametricBlock)(double); @interface CAKeyframeAnimation (Parametric) + (id)animationWithKeyPath:(NSString *)path function:(KeyframeParametricBlock)block fromValue:(double)fromValue toValue:(double)toValue; </code></pre> <p>CAKeyframeAnimation+Parametric.m:</p> <pre><code>@implementation CAKeyframeAnimation (Parametric) + (id)animationWithKeyPath:(NSString *)path function:(KeyframeParametricBlock)block fromValue:(double)fromValue toValue:(double)toValue { // get a keyframe animation to set up CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:path]; // break the time into steps // (the more steps, the smoother the animation) NSUInteger steps = 100; NSMutableArray *values = [NSMutableArray arrayWithCapacity:steps]; double time = 0.0; double timeStep = 1.0 / (double)(steps - 1); for(NSUInteger i = 0; i &lt; steps; i++) { double value = fromValue + (block(time) * (toValue - fromValue)); [values addObject:[NSNumber numberWithDouble:value]]; time += timeStep; } // we want linear animation between keyframes, with equal time steps animation.calculationMode = kCAAnimationLinear; // set keyframes and we're done [animation setValues:values]; return(animation); } @end </code></pre> <p>Now usage will look something like this:</p> <pre><code>// define a parametric function KeyframeParametricBlock function = ^double(double time) { return(1.0 - pow((1.0 - time), 2.0)); }; if (layer) { [CATransaction begin]; [CATransaction setValue:[NSNumber numberWithFloat:2.5] forKey:kCATransactionAnimationDuration]; // make an animation CAAnimation *drop = [CAKeyframeAnimation animationWithKeyPath:@"position.y" function:function fromValue:30.0 toValue:450.0]; // use it [layer addAnimation:drop forKey:@"position"]; [CATransaction commit]; } </code></pre> <p>I know it might not be quite as simple as what you wanted, but it's a start.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload