问题描述
我有一个CAEmitterCell
,并且已将其设置为特定的颜色.该文档说此属性是可动画的,我想为游戏中的不同玩家(所有人都在开始时选择他们的颜色)在多种不同颜色之间进行动画设置.
I have a CAEmitterCell
and I have set it up with a specific colour. The documentation says that this property is animatable, and I would like to animate this between a number of different colours for the different players in my game (all of whom choose their color at the start).
这是我设置的EmitterCell
:
//
// Emitter Cells
//
// 'New Emitter' cell configuration
newEmitter = [CAEmitterCell emitterCell];
newEmitter.scaleSpeed = 10;
newEmitter.lifetime = 2.481715;
newEmitter.velocity = 332.3636968085106;
newEmitter.contents = newemitterImg;
newEmitter.name = @"New Emitter";
newEmitter.color = [[UIColor colorWithRed:0.50 green:0.00 blue:1.00 alpha:1.00] CGColor];
newEmitter.scaleRange = 4.178236607142859;
newEmitter.lifetimeRange = 1.6;
newEmitter.greenRange = -2.775558e-17;
newEmitter.birthRate = 40;
newEmitter.emissionRange = -6.283185306666667;
newEmitter.scale = 0;
//
// Emitter Cell Chains
//
emitter.emitterCells = [NSArray arrayWithObjects:newEmitter, nil];
在这里,我正在测试这种情况下的颜色变化,只是在两种不同的颜色之间跳动:
And here is where I am testing the color change in this case just bouncing between two different colors:
-(void)changeColor {
if (color == 0) {
color = 1;
NSLog(@"color = 1");
[UIView animateWithDuration:1.5 delay:0 options:0 animations:^{
newEmitter.color = [[UIColor colorWithRed:0.50 green:0.00 blue:1.00 alpha:1.00] CGColor];
} completion:^(BOOL finished) {[self performSelector:@selector(changeColor) withObject:nil afterDelay:2];}];
} else {
color = 0;
NSLog(@"color = 0");
[UIView animateWithDuration:1.5 delay:0 options:0 animations:^{
newEmitter.color = [[UIColor colorWithRed:1.00 green:0.50 blue:0.10 alpha:1.00] CGColor];
} completion:^(BOOL finished) {[self performSelector:@selector(changeColor) withObject:nil afterDelay:2];}];
}
}
但是,当我执行此操作时,颜色永远不会改变.我在这里误解了"可动画"的性质,还是只需要对CAEmitterCell
进行不同的处理?
However, when I run this the color never changes. Have I misunderstood the nature of "Animatable" here, or do I just need to go about it differently for a CAEmitterCell
?
推荐答案
CAEmitterCell实际上是不同的.要使动画生效,您需要执行以下步骤:
CAEmitterCells are, in fact, different. To get animations working on them, you need to take these steps:
1.为您的CAEmitterCell分配一个名称,例如:
1.Assign a name to your CAEmitterCell, e.g.:
newEmitter.name = @"fire";
2.通过CAEmitterLayer实例访问此发射器的动画属性:
2.Access the animation property for this emitter through the CAEmitterLayer instance:
//Set first before doing CABasicAnimation so it sticks
newEmitter.redSpeed = 1.0;
//Access the property with this key path format: @"emitterCells.<name>.<property>"
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"emitterCells.fire.redSpeed"];
anim.fromValue = @(0.0);
anim.toValue = @(1.0);
anim.duration = 1.5;
anim.fillMode = kCAFillModeForwards;
[emitter addAnimation:anim forKey:@"emitterAnim"];
这篇关于动画CAEmitterCell Color属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!