Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CATransform3DRotate rotate for 360 degrees

I've started using CATransform3D lately and it seems very nice. I just have 1 issue with the rotation though. I'm trying to rotate my view for 360˚ degrees to the right but if I just put pass 360 to CATransform3DRotate it doesn't work (It just doesn't move at all.)

Here's my code:

      CALayer *layer = dock.layer;
      CATransform3D r = CATransform3DIdentity;
      r.m34 = 1.0 / -500;
      r = CATransform3DRotate(r, DegreesToRadians(360.0f), 100.0f, 1.0f, 100.0f);   
      layer.transform = r;

Does anyone know how to fix this issue? Thanks in advance! :)

like image 385
David Murray Avatar asked Jul 20 '12 00:07

David Murray


1 Answers

You can animate a layer through one (or more) full rotations around its Z axis by animating the layer's transform.rotation key path, like this:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.duration = .25;
animation.fromValue = [NSNumber numberWithFloat:0];
animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
[layer addAnimation:animation forKey:animation.keyPath];

You can animate around the X or Y axes using the key paths transform.rotation.x and transform.rotation.y. (The transform.rotation.z key path has the same effect as the transform.rotation key path.) You can apply multiple rotations, on separate axes, simultaneously.

Another way to do it, which probably works better if you want to rotate around an off-axis vector, is using a keyframe animation, like this:

CALayer *layer = [sender layer];
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0 / -50;
layer.transform = transform;

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.values = [NSArray arrayWithObjects:
    [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 0 * M_PI / 2, 100, 1, 100)],
    [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 1 * M_PI / 2, 100, 1, 100)],
    [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 2 * M_PI / 2, 100, 1, 100)],
    [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 3 * M_PI / 2, 100, 1, 100)],
    [NSValue valueWithCATransform3D:CATransform3DRotate(transform, 4 * M_PI / 2, 100, 1, 100)],
    nil];
animation.duration = 2;
[layer addAnimation:animation forKey:animation.keyPath];
like image 197
rob mayoff Avatar answered Sep 24 '22 10:09

rob mayoff