问题
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).
Here is my EmitterCell
when I set it up:
//
// 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];}];
}
}
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
?
回答1:
CAEmitterCells are, in fact, different. To get animations working on them, you need to take these steps:
1.Assign a name to your CAEmitterCell, e.g.:
newEmitter.name = @"fire";
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"];
回答2:
Try this:
[newEmitter.emitterCells[0] setColor:[[UIColor yellowColor] CGColor]];
回答3:
You can use those properties to animate emiter color.
newEmitter.redSpeed = 1.0;
newEmitter.greenSpeed = 1.0;
newEmitter.blueSpeed = 1.0;
来源:https://stackoverflow.com/questions/15961910/animating-the-caemittercell-color-property