Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a CAEmitterCell fade out at the end of its lifetime?

I'm animating some particles and rather than have then just disappear at the end of their lifetime I'd like them to fade out.

I have a CAEmitterCell defined with a lifetime of 35.0. I don't want to just have the particle fade out over the full duration of the particle lifetime. I only want it to fade out at the end. Perhaps the last 2 or 3 seconds.

like image 867
Jeff Avatar asked Nov 28 '11 02:11

Jeff


2 Answers

For the CAEmitterCell's color property, set the alpha value to lifetime * alphaSpeed (where alphaSpeed is -1.0/fadeOutDuration).

So for a lifetime of 35.0 and a fadeOutDuration of 2.0, alphaSpeed would be -0.5, and alpha would be 17.5.

There are a couple caveats:

  1. This only works if your cells are supposed to start at full alpha.
  2. You'll have to set CAEmitterCell's color property using a CGColorRef created with CGColorCreateCopyWithAlpha. Both UIColor and CGColorCreate clamp their values to a maximum of 1.0. For whatever reason, CGColorCreateCopyWithAlpha doesn't.
like image 59
Walter Avatar answered Oct 31 '22 19:10

Walter


Just came across this in the docs which might point in the right direction:

name The name of the cell.

@property(copy) NSString *name Discussion The cell name is used when constructing animation key paths that reference the cell. Defaults to nil.

For example, adding an animation to a cell’s enclosing layer with the a keypath such as emitterCells.myCellName.redRange would animate the redRange propery of the cell in the layer’s emitterCells array with the name myCellName.

Availability Available in Mac OS X v10.6 and later. Declared In CAEmitterCell.h

I gather you still add the animation to the layer but with a key path that references a part of that layer - in this case the cell's property. Is there an alpha property exposed for cells?

like image 21
Hari Honor Avatar answered Oct 31 '22 20:10

Hari Honor