2012-07-13 211 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