粒子效果的应用场景:
- 主播房间右下角粒子动画
- 雪花/下雨/烟花等效果
- QQ生日快乐一堆表情的跳动
常规实现方法(核心代码):
import Foundation
import UIKit
extension Particleable where Self : UIViewController {
func addParticleEffect(_ point : CGPoint = CGPoint(x: UIScreen.main.bounds.width * 0.85, y: UIScreen.main.bounds.height - 20) ) {
// 1.创建发射器
let emitter = CAEmitterLayer()
// 2.发射器位置
emitter.emitterPosition = point
// 3.开启三维效果
emitter.preservesDepth = true
var cells = [CAEmitterCell]()
for i in 0..<10 {
// 4.设置 Cell(对应其中一个粒子)
// 4.0.创建粒子
let cell = CAEmitterCell()
// 4.1.每秒发射粒子数
cell.birthRate = Float(arc4random_uniform(4)) + 3
// 4.2.粒子存活时间
cell.lifetime = 5
cell.lifetimeRange = 2.5
// 4.3.缩放比例
cell.scale = 0.7