1

我有一个问题CAAnimationGroup。我有一个名为的视图animeView,我想同时应用围绕 Y 轴的 3D 旋转和缩放变换。旋转和缩放动画分别工作得非常好!但是当我将它们添加到CAAnimationGroup非动画中时!

问题是什么?

CAKeyframeAnimation *rotation = [CAKeyframeAnimation animation];
CATransform3D rotationTransform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
rotationTransform.m34 = 1.0 / -500;

rotation.values = [NSArray arrayWithObjects:
                   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 0.0f, 1.0f, 0.0f)],
                   [NSValue valueWithCATransform3D:rotationTransform],
                   [NSValue valueWithCATransform3D:CATransform3DMakeRotation(2.0*M_PI, 0.0f, 1.0f, 0.0f)] ,nil];

CAKeyframeAnimation *trans = [CAKeyframeAnimation animation];

trans.values = [NSArray arrayWithObjects:[NSValue valueWithCATransform3D:CATransform3DIdentity],[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 0.5)],[NSValue valueWithCATransform3D:CATransform3DIdentity], nil];

CAAnimationGroup *group = [CAAnimationGroup animation];

group.delegate = self;
[group setAnimations: [NSArray arrayWithObjects:rotation, trans, nil]];
group.duration = 2;
group.repeatCount = 3;

[self.animView.layer addAnimation:group forKey:nil];
4

1 回答 1

2

您提供的代码有两点奇怪。

  1. 动画不知道他们应该改变什么属性(没有设置关键路径)
  2. 两个动画都在改变变换(应该真正使用什么值?)

我看到的方式是您有两个具有相同数量值(三个)的变换关键帧动画。因此,您应该计算总变换矩阵(在一个矩阵中缩放和旋转),并且只将该动画添加到您的视图中。

它看起来像这样(我添加了很多评论来解释 sis 发生了什么):

// Your first rotation is 0 degrees and no scaling
CATransform3D beginTransform = CATransform3DIdentity;

// Your middle rotation is the exact one that you provided ...
CATransform3D middleTransform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
// ... but the rotation matrix is then scaled as well ...
middleTransform = CATransform3DScale(middleTransform, 0.5, 0.5, 0.5);
// ... and the perspective is set
middleTransform.m34 = 1.0 / -500;

// Your end value is rotated but not scaled
CATransform3D endTransform = CATransform3DMakeRotation(2.0*M_PI, 0.0f, 1.0f, 0.0f);

// Create a new animation that should animate the "transform" property (thus the key path "transform")
CAKeyframeAnimation *rotateAndScale = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
// same configurations as your group had
[rotateAndScale setDuration:2.0];
[rotateAndScale setRepeatDuration:3.0];
[rotateAndScale setDelegate:self];
// Set the values of the transform animation (yes, both scaling and rotation in one animation)
[rotateAndScale setValues:[NSArray arrayWithObjects:
                           [NSValue valueWithCATransform3D:beginTransform], 
                           [NSValue valueWithCATransform3D:middleTransform], 
                           [NSValue valueWithCATransform3D:endTransform], nil]];

// Add the animation to the layer, no need for a group here...
[animView.layer addAnimation:rotateAndScale forKey:nil];
于 2012-07-15T09:21:21.300 回答