0

I'm attempting to create an animated CALayer that uses a CABasicAnimation in order to animate an object that rotates at discrete intervals between two angles. Note the word discrete: I'm not trying to create a continuous animation between two points, but rather I want to calculate fixed increments between each angle in order to create a discrete movement feel.

Here's a diagram:

enter image description here

I've looked into setting the byValue attribute of the CABasicAnimation, but I can't seem to get it to work since you can't use fromValue, ToValue, and byValue in one animation. fromValue is always from zero, so I guess that could be dropped, but it still never animates correctly when using the current endAngle as the toValue and a byValue of 0.1 (arbitrarily chosen but should work for testing). Any ideas on how to implement this? Here's code I'm using:

anim = [CABasicAnimation animationWithKeyPath:@"currentEndAngle"];
anim.duration = 0.5f;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim.toValue = [NSNumber numberWithFloat:self.endAngle];
anim.byValue = [NSNumber numberWithFloat:0.1f];
anim.repeatCount = HUGE_VALF;
[anim setDelegate:self];
[self addAnimation:anim forKey:@"animKey"];

Thanks!

J Kagney
  • 239
  • 1
  • 4
  • 9
  • Do you want it to move `0.1f` each step? `byValue` won't work as it has different meaning: if you set `toValue` and `byValue`, the animation will go from `toValue - byValue` to `toValue`. I guess you need a timer here... – Khanh Nguyen Jun 23 '13 at 02:41
  • Check out the `CABasicAnimation` documentation – Khanh Nguyen Jun 23 '13 at 02:41
  • Yes, I want it to move 0.1 radians each step. Are you saying CABasicAnimation won't cut it for this? – J Kagney Jun 23 '13 at 02:42
  • 1
    No it won't, it doesn't support discreet animation... But you may want to have a look at `CAKeyframeAnimation`. – Khanh Nguyen Jun 23 '13 at 03:44
  • When it comes down to it, an NSTimer that infinitely loops sounds like it'll get the job done. Thanks for the suggestion! – J Kagney Jun 23 '13 at 03:45
  • `CAKeyframeAnimation` is the way to go – Wain Jun 23 '13 at 08:16
  • I've implemented the NSTimer solution and for trivially small animations it works, but when you're animating large amounts of primitives, the whole thing breaks down because the only way to get it to run through all of them is to set the time extremely close to zero, which ends up undercutting the current frame being drawn. I'll have to implement CAKeyframeAnimation. – J Kagney Jun 23 '13 at 20:04
  • Also, CADisplayLink seems to be another thing to consider here. – J Kagney Jun 23 '13 at 20:14

0 Answers0