使用CAAnimationGroup对两个核心动画进行分组会导致一个CABasicAnimation无法运行

Chr*_*ian 8 iphone core-animation

我有两个动画,我试图在iPhone上使用OS 3.1.2在UILabel上执行.第一个来回摇晃UILabel:

CAKeyframeAnimation *rock;
rock = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
[rock setBeginTime:0.0f];
[rock setDuration:5.0];
[rock setRepeatCount:10000];

NSMutableArray *values = [NSMutableArray array];
MovingMath *math = [[MovingMath alloc] init];

// Center start position
[values addObject:[math DegreesToNumber:0]];

// Turn right
[values addObject:[math DegreesToNumber:-10]];

// Turn left
[values addObject:[math DegreesToNumber:10]];

// Re-center
[values addObject:[math DegreesToNumber:0]];

// Set the values for the animation
[rock setValues:values];

[math release];
Run Code Online (Sandbox Code Playgroud)

第二个缩放UILabel使其变大:

NSValue *value = nil;
CABasicAnimation *animation = nil;
CATransform3D transform;
animation = [CABasicAnimation animationWithKeyPath:@"transform"];
transform = CATransform3DMakeScale(3.5f, 3.5f, 1.0f);
value = [NSValue valueWithCATransform3D:transform];
[animation setToValue:value];
transform = CATransform3DMakeScale(1.0f, 1.0f, 1.0f);
value = [NSValue valueWithCATransform3D:transform];
[animation setFromValue:value];
[animation setAutoreverses:YES];
[animation setDuration:30.0f];
[animation setRepeatCount:10000];
[animation setBeginTime:0.0f];
Run Code Online (Sandbox Code Playgroud)

将这些动画中的任何一个直接添加到UILabel的图层都可以正常工作.

但是,如果我尝试将动画组合在一起,则第一个"摇摆"动画不起作用:

CAAnimationGroup *theGroup = [CAAnimationGroup animation];

theGroup.duration = 5.0;
theGroup.repeatCount = 10000;
theGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
theGroup.animations = [NSArray arrayWithObjects:[self rockAnimation], [self zoomAnimation], nil]; // you can add more

// Add the animation group to the layer
[[self layer] addAnimation:theGroup forKey:@"zoomAndRotate"];
Run Code Online (Sandbox Code Playgroud)

将动画添加到组的顺序无关紧要.我没有按照上面的方式进行缩放,而是尝试更改边界,但这也不成功.任何见解将不胜感激.谢谢.

Bra*_*son 8

您正在尝试同时为两个属性设置动画,即CALayer的变换.在第一个动画中,您使用辅助键路径来更改变换以生成旋转,在第二个动画中,您将直接更改变换以生成缩放.第二个动画将覆盖第一个动画,因为您正在构建仅仅缩放并在它们之间制作动画的整个变换.

看起来您可以通过使用两个动画的辅助键路径来同时进行图层的缩放和旋转.如果您在缩放动画上更改代码以进行读取

CABasicAnimation *animation = nil;
animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
[animation setToValue:[NSNumber numberWithDouble:3.5]];
[animation setFromValue:[NSNumber numberWithDouble:1.0]];
[animation setAutoreverses:YES];
[animation setDuration:30.0f];
[animation setRepeatCount:10000];
[animation setBeginTime:0.0f];
Run Code Online (Sandbox Code Playgroud)

你应该能够在你的图层上进行摇摆和缩放.