Rotations with CABasicAnimation

When you use CABasicAnimation to rotate a layer, there are some annoying features to watch out for (annoying to me, anyway). For one, if you specify a transform with a rotation of 360 degrees (translated to radians), Core Animation will helpfully take the shortest path. That is to say, it will do nothing.

I guess I shouldn’t complain. After all, if I made a long, complex circuit and then allowed the positive and negative leads to occupy the same space, I should not expect anything better than a short circuit.

The best solution is the code provided by Jason Medeiros on this stackoverflow topic.

Other solutions I have tried involved repeating a rotation of 180 degrees or less several times using the repeatCount property. It looks like another poster took a similar approach. The problem with this approach is that there is a slight hiccup between repeatcounts. It seems to be more noticeable with larger images, probably because the object is jumping back by some fractional angle between rotations.

Edit: getting a little more specific, it looks something like this:

[theLayer addAnimation:(CABasicAnimation*)[self animationForRocks] forKey:@"animateLayer"];

- (id)animationForRocks {

CABasicAnimation* spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spinAnimation.toValue = [NSNumber numberWithFloat:.01*2*M_PI];
spinAnimation.repeatCount = 10000;
spinAnimation.cumulative = YES;

}
This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. admin
    Posted May 23, 2010 at 2:38 am | Permalink

    Another thing that might make a difference is:

    animateZRotation = [CABasicAnimation animationWithKeyPath:@"transform"];

    vs

    animateZRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];

    The latter is used when setting the toValue of the transform, which works smoothly. The former would be useful for combining transforms, but my implementations have often included the aforementioned “jumps.”

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>