#### Introduction to particle system

  • What is a particle system?
    • Particle system is a collection of large number of display elements which have the same performance law in the whole, but randomly show different characteristics.
  • The definition of a particle has three elements
    • Mass: Particle systems are made up of a “mass of display elements” (snow, rain, a cloud of fog, etc.)
    • Unity: Each element of a particle system behaves the same way (e.g. rain, snow, direction from top to bottom)
    • Randomness: Each element of a particle system behaves randomly in a different way (e.g. in snow, each snowflake falls at a different speed, has a different size, and goes in a slightly different direction)

#### Particle system application scenario

  • The film and television industry
    • At present, we often see big movies, such as sandstorms, storms, snowstorms, fireworks and so on, many of them are made by particle effects. It’s realistic, and it’s much cheaper to shoot in the real world
  • In the game
    • Explosions, rain, snow, fog, fireworks and so on. It’s all particle system implementations
  • In the application
    • Particle animation in the lower right corner of anchor room
    • Snow/rain/fireworks, etc
    • QQ happy birthday a bunch of expression beat

####iOS particle animation use

  • steps
    • Creating an emitter
    • Create particle, set particle properties
  • Swift core code
// 1. Create an emitterletemitter = CAEmitterLayer() // 2. EmitterPosition = Point // 3 Open 3d effect Emitter. PreservesDepth =trueVar cells = [CAEmitterCell]()for i in0.. <10 {// 4.1. Create sample CellletCell = CAEmitterCell() // 4.2. Set particle velocity cell.velocity = 150 cell.velocityRange = 100 // 4.3. Scale = 0.7 cell.scaleRange = 0.3 // 4.4. Set the particle direction cell.emissionlongitude = CGFloat(-double-.pi /2) cell.emissionrange = CGFloat(double-.pi / 2/6) // 4.5. Lifetime = 3 cell.lifetimeRange = 1.5 // 4.6 Spin = CGFloat(double-.pi /2) Cell. spinRange = CGFloat(double-.pi / 2/2) // 4.6. BirthRate = 2 // 4.7 Cell.contents = UIImage(named:"good\(i)_30x30")?.cgImage
            
            // 4.8.添加到数组中
            cells.append(cell)
        }
        
        // 5.将粒子设置到发射器中
        emitter.emitterCells = cells
        
        // 6.将发射器的layer添加到父layer中
        view.layer.addSublayer(emitter)

Copy the code

#### Particle animation effect display

#### source GitHub address: ParticleAnimationDemo