使用CAAnimationGroup对两个核心动画进行分组会导致一个CABasicAnimation无法运行 [英] Grouping two Core Animations with CAAnimationGroup causes one CABasicAnimation to not run

查看:124
本文介绍了使用CAAnimationGroup对两个核心动画进行分组会导致一个CABasicAnimation无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two animations that I'm trying to perform on a UILabel on the iPhone with OS 3.1.2. The first rocks the UILabel back and forth:

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

第二个缩放UILabel使其变大:

The second zooms the UILabel so that it becomes larger:

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

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

Adding either one of these animations directly to the UILabel's layer works as expected.

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

However, if I try to group the animations together, the first "rocking" animation does not function:

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"];

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

The order of adding the animations to the group does not matter. Instead of zooming, in the manner above, I tried changing the bounds, but that was unsuccessful as well. Any insight would be greatly appreciated. Thank you.

推荐答案

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

You are attempting to simultaneously animate two changes to one property, your CALayer's transform. In the first animation, you are using a helper keypath to change the transform to produce a rotation, and in the second you are changing the transform directly to produce a scaling. The second animation is overwriting the first, because you are constructing whole transforms that are only scaled and animating between them.

看来你可以同时进行缩放和旋转通过使用两个动画的辅助键路径来发生图层。如果您将缩放动画上的代码更改为

It appears that you can cause both scaling and rotation of your layer to occur by using helper keypaths for both animations. If you change your code on the scaling animation to read

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

你应该能够在你的图层上同时摇摆和缩放。

you should be able to have both rocking and scaling on your layer.

这篇关于使用CAAnimationGroup对两个核心动画进行分组会导致一个CABasicAnimation无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆