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

あなたが提供したコードには奇妙なことが2つあります。

  1. アニメーションは、変更する必要のあるプロパティを認識していません(キーパスが設定されていません)
  2. 両方のアニメーションが変換を変更しています(実際にどの値を使用する必要がありますか?)

私の見方では、同じ数の値(3つ)を持つ2つの変換キーフレームアニメーションがあります。したがって、変換行列全体(1つの行列でスケールと回転の両方)を計算し、そのアニメーションのみをビューに追加する必要があります。

これは次のようになります(何が起こっているかを説明するためにたくさんのコメントを追加しました):

// 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 に答える