animationCAEmitterCell颜色属性

我有一个CAEmitterCell ,我已经设置了一个特定的颜色。 文档说, 这个属性是可以animation的 ,我想为我的游戏中的不同玩家(所有人在开始时select他们的颜色)在不同的颜色之间进行animation处理。

当我设置它时,这是我的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]; 

这里是我testing颜色变化的地方,只是在两种不同的颜色之间跳动:

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

但是,当我运行这个颜色永远不会改变。 我在这里误解了“ Animatable ”的性质,还是仅仅需要对CAEmitterCell不同的CAEmitterCell呢?

事实上,CAEmitterCells是不同的。 要获得animation效果,您需要执行以下步骤:

1.为您的CAEmitterCell分配一个名称,例如:

newEmitter.name = @"fire";

2.通过CAEmitterLayer实例访问此发射器的animation属性:

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

尝试这个:

  [newEmitter.emitterCells[0] setColor:[[UIColor yellowColor] CGColor]]; 

您可以使用这些属性来为发射器颜色添加animation。

 newEmitter.redSpeed = 1.0; newEmitter.greenSpeed = 1.0; newEmitter.blueSpeed = 1.0;