CATransform3DRotate rotate for 360 degrees
Asked Answered
C

1

6

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! :)

Concurrent answered 20/7, 2012 at 0:54 Comment(2)
Are you trying to rotate 180 or 360 degrees? Because I think rotating 360 degrees is the same as not rotating at all.Uund
@Uund I think he's trying to animate his layer through a full 360 degree rotation.Jehiel
J
23

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];
Jehiel answered 20/7, 2012 at 2:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.