CAAnimationGroup无法正常工作

时间:2012-07-13 07:34:21

标签: ios animation core-animation caanimation

我对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];

1 个答案:

答案 0 :(得分:2)

您提供的代码有两件事情很奇怪。

  1. 动画不知道应该更改哪些属性(没有设置关键路径)
  2. 两个动画都在改变变换(应该使用什么值?)
  3. 我看到它的方式是你有两个具有相同数量值的变换关键帧动画(三个)。因此,您应该计算总转换矩阵(在一个矩阵中进行缩放和旋转),并仅将该动画添加到视图中。

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

    // 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];
    
相关问题