6

我最近开始使用 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;

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

4

1 回答 1

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.x您可以使用关键路径和围绕 X 或 Y 轴制作动画transform.rotation.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];
于 2012-07-20T02:58:49.087 回答