2012-07-13 216 views
1

我遇到了问题CAAnimationGroup。我有一个名为animeView的视图,我想同时应用围绕Y轴的3D旋转和缩放转换。旋转和缩放动画效果很好分开!但是当我将它们添加到CAAnimationGroup非动画发生!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]; 
+0

你永远设置,您设置动画的关键路径。此外,你不应该动画变换两次 – 2012-07-13 07:38:01

+0

我不明白关键路径。它是否就像动画的标签?它是否必须是特定于每种转换的东西? – Armin 2012-07-15 00:07:18

+0

顺便说一句,我不是动画变换两次。这些应该同时发生,这就是为什么我使用CAAnimationGroup! – Armin 2012-07-15 00:08:43

回答

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

感谢大卫,我明白现在它如何运作得更好。 – Armin 2012-08-11 18:26:28