Some of our common like snow, rain, fire such as animation, can use particle effect to achieve. Classes CAEmitterLayer and CAEmitterCell are mainly used to achieve this. Let’s illustrate how this class is used and what the properties mean by implementing a snow effect.

CAEmitterLayer can be understood as a particle emitter

Create an instance of CAEmitterLayer, that is, create an emitter.

CGRect rect = CGRectMake(0, 100, self.view.bounds.size.width, 50); CAEmitterLayer *emitter = [CAEmitterLayer layer]; emitter.frame = rect; [self.view.layer addSublayer:emitter];Copy the code

Setting the Emitter Type

// Set emitter type Emitter. EmitterShape = kCAEmitterLayerRectangle;Copy the code

So that’s where the emitterShape property comes in. There are six values: kCAEmitterLayerPoint, kCAEmitterLayerLine, kCAEmitterLayerRectangle, kCAEmitterLayerCuboid, kCAEmitterLayerCircle, and kCAEm ItterLayerSphere. A brief introduction to several

  • KCAEmitterLayerPoint is set to this type and all particles of the emitter will be created from the same point. This point is the position of the emitter.

  • KCAEmitterLayerLine is set to this value and all particles will be created at the top of the emitter.

  • KCAEmitterLayerRectangle is set to this value, and all particles are created in the entire region of the emitter.

Forget the other three. Note that the region mentioned above is not for emitterLayer, but is confirmed by the attributes emitterPosition and emitterSize. Let’s set the values of these two properties, in this case, to be the same as layer.

Emitter. EmitterPosition = CGPointMake (the rect. Size. Width * 0.5, the rect. Size, height * 0.5); emitter.emitterSize = rect.size;Copy the code

CAEmitterCell can be understood as an example that we want to send

Like the Animation we mentioned before, CAEmitterCell is just a data model that we use to show you the shape of particles.

We create a particle with the image fla.png as its content. The emitter then creates many copies of it to simulate real snowflakes.

CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
emitterCell.contents = (__bridge id _Nullable)([UIImage imageNamed:@"flake"].CGImage);
Copy the code

And then I add the code

Emittercell. birthRate = 20; // Set particle creation rate, which is the number of birthrates per second. Emittercell. lifetime = 3.5; emitterCell.lifetime = 3.5; EmitterCells = @[emitterCells];Copy the code

The emitterCells property that needs to be explained. We set the emitterCells property to an array that holds data of type CAEmitterCell. The same emitter can emit different types of particles.

This time we run the program and we’ll see something like this

We found that although particles are produced, they do not move. So keep adding code

BirthRate = 200; // birthRate = 200; // birthRate = 200; Emittercell. lifetime = 3.5; emitterCell.lifetime = 3.5; / / set the particle statement cycle range emitterCell. LifetimeRange = 1.0; EmitterCells = @[emitterCells]; / / set on the y axis acceleration emitterCell yAcceleration = 70.0 f; / / set on the x axis acceleration emitterCell xAcceleration = 10.0 f; // Set the initial velocity of the particle emittercell. velocity = 20; / / set the initial Angle of particles If you do not set this value, we found that the particles are horizontal launch emitterCell. EmissionLongitude = - M_PI_2; / / set the initial velocity of the particles In this case the range is - 180 ~ 220 emitterCell. VelocityRange = 200.0 f; / / set the particle's initial Angle range The scope of this example is M_PI ~ 0 emitterCell. EmissionRange = M_PI_2; // Set the particle color emittercell. color = [UIColor colorWithRed:0.9 green:1.0 blue:1.0 alpha:1.0]. // We can also set random colors and limit the range. Since RGB has a maximum value of 1.0, for Red, the range is not 0.6 to 1.3, but 0.6 to 1.0. Similarly, if it is negative, the minimum value is 0 emitterCell.redrange = 0.3; EmitterCell. GreenRange = 0.3; EmitterCell. BlueRange = 0.3; // Set the particle size and its size range emittercell.scale = 0.8; EmitterCell. ScaleRange = 0.8; // Set the particle size to decrease by 15% per second over time, or increase emitterCell.scaleSpeed = -0.15 per second if set to positive; // Set the particle transparency range emitterCell. AlphaRange = 0.75; // Set the particle change speed emittercell. alphaSpeed = -0.15;Copy the code

A lot of attributes are range related types, all specified ranges, not written individually. End result:

Making the address