核心动画:为什么我不能合成两个动画?

时间:2010-11-24 22:50:17

标签: animation ios core-animation

我正在尝试为车轮设置动画

当手指停留在> 0.1秒,轮子'弹出'即我将Scale从1.0动画到1.2。

当手指移动时,轮子随之旋转(只有当它弹出时)。

但是我遇到了一个问题:一旦开始旋转,比例就会缩小到1.0。

- (void) spin: (Direction) direction
{
 float thetaOld = thetaWheel;

 float k = (direction == AntiClockwise) ? -1 : 1;
 float t = 2 * M_PI * ( k / 12.0 );
 thetaWheel += t;

 CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath:@"transform"];

 anim.fromValue = [NSNumber numberWithDouble: thetaOld];
 anim.toValue = [NSNumber numberWithDouble: thetaWheel];

 anim.valueFunction = [CAValueFunction functionWithName: kCAValueFunctionRotateZ];

 anim.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; 

 anim.duration = 0.1f;
 anim.fillMode = kCAFillModeForwards;
 anim.removedOnCompletion = NO;

 [self.wheelLayer addAnimation: anim forKey:@"transform"];
}

- (void) popOut
{
 CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

 anim.fromValue = [NSNumber numberWithDouble: 1.0];
 anim.toValue = [NSNumber numberWithDouble: 1.2];

 anim.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; 

 anim.duration = 0.25f;
 anim.fillMode = kCAFillModeForwards;
 anim.removedOnCompletion = NO;

 [self.wheelLayer addAnimation: anim forKey:@"transform.scale"];
}

// doesnt get used yet
- (void) popBack
{
 CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

 anim.fromValue = [NSNumber numberWithDouble: 1.2];
 anim.toValue = [NSNumber numberWithDouble: 1.0];

 anim.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; 

 anim.duration = 0.1f;
 anim.fillMode = kCAFillModeForwards;
 anim.removedOnCompletion = NO;

 [self.wheelLayer addAnimation: anim forKey:@"transform.scale"];
}

此时最后一个例程没有被使用!所以我看不出是什么让规模缩小到1 ......

看起来不知何故,第二个动画正在覆盖第一个动画......

如何正确地做到这一点?

1 个答案:

答案 0 :(得分:2)

我猜(尚未测试过)在动画中应用的'变换'keyPath的图层变换会覆盖您在缩放动画中应用的变换。

对于旋转,请尝试使用带有transform.rotation.z密钥路径的动画(在这种情况下您不需要设置valueFunction)