CATransform3DRotate旋转360度

时间:2012-07-20 00:54:20

标签: iphone objective-c calayer catransform3d quartz-core

我最近开始使用CATransform3D,看起来非常好。我只有旋转1个问题。我正在尝试将我的视图向右旋转360度,但如果我只是将360传递给CATransform3DRotate它就不起作用(它根本不会移动。)

这是我的代码:

      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;

有谁知道如何解决这个问题?提前致谢! :)

1 个答案:

答案 0 :(得分:23)

您可以通过设置图层的transform.rotation关键路径来围绕其Z轴完成一个(或多个)完整旋转动画,如下所示:

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];

您可以使用关键路径transform.rotation.xtransform.rotation.y围绕X轴或Y轴设置动画。 (transform.rotation.z键路径与transform.rotation键路径具有相同的效果。)您可以同时在不同的轴上应用多个旋转。

另一种方法,如果你想围绕一个离轴矢量旋转可能效果更好,就是使用关键帧动画,如下所示:

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];