Particle system
- Results the following
—
Introduction to particle System
- What is a particle system?
- Particle system is a set composed of a large number of display elements that have the same performance law on the whole but show different characteristics randomly.
- The definition of a particle has three elements
- Swarm: A particle system is composed of a “mass of display elements” (e.g., snow, rain, a cloud of fog, etc.)
- Unity: A particle system in which each element behaves in the same way (e.g., when it rains, when it snows, the direction is all up and down)
- Randomness: The random behavior of each element of the particle system (snow, for example, each snowflake falls at a different speed, in a different size and in a slightly different direction)
Particle system application scenarios
- The film and television industry
- At present, we often watch big movies, such as sandstorm, storm, blizzard, fireworks and so on, many of them are made by particle effects. It’s realistic, and it costs a lot less than the real world
- In the game
- Explosions, rain, snow, fog, fireworks and so on. Both are particle system implementations
- In the application
- Lower right corner of anchor room particle animation
- Snowflakes/rain/fireworks etc
- QQ happy birthday a bunch of expression beat
The use of particle systems
- steps
- Create emitter
- Create the particle and set the particle properties
- code
Let emitter = CAEmitterLayer() //2. Emitter = CAEmitterLayer() EmitterPosition = CGPoint(x: view.bounds.width * 0.5, Y: -60) //3. Emitter. PreservesDepth = True //4. Create a particle, Let cell = CAEmitterCell() //4.2 Set particle velocity = 150 cell.velocityRange = 100 // 4.3 Scale = 0.7 cell.scaleRange = 0.3 // 4.4. EmissionLongitude = CGFloat(double-.pi /2) Cell.EmissionRange = CGFloat(double-.pi / 4) // 4.5. Lifetime = 6 cell.lifetimeRange = 1.5 // 4.6. Spin = CGFloat(double-.pi /2) cell. SpinRange = CGFloat(double-.pi / 4) // 4.6. Cell. BirthRate = 20 // 4.7. Cell. contents = UIImage(named: "good6_30x30")? .cgimage // 5. Emitter. EmitterCells = [cell] // 6 View.layer.addsublayer (Emitter)} func Stop () {/* for layer in view.layer.sublayers! { if layer.isKind(of: CAEmitterLayer.self) { layer.removeFromSuperlayer() } } */ view.layer.sublayers? .filter({ $0.isKind(of: CAEmitterLayer.self)}).first? .removeFromSuperlayer() }Copy the code
- Extension to create multiple image styles
Var Cells = [CAEmitterCell]() for I in 0.. Cell. velocity = 150 cell.velocityRange = 100 //4.3 Set particle size Cell. scale = 0.7 cell.scaleRange = 0.3 //4.4 Set the direction of the particle Cell. emissionLongitude = CGFloat(-double-.pi /2) cell.emissionRange = Lifetime = 3 cell.lifetimeRange = 1.5 //4.6 Set cell. Spin = CGFloat(Double. PI /2) cell. SpinRange = CGFloat(Double. PI /4) cell cell.contents = UIImage(named: "good\(i)_30x30")? Cells.append (cell)} // 5. Set the particle to an emitter emitter. EmitterCells = CellsCopy the code