粒子动画
func test() {
self.view.backgroundColor = UIColor.black
let icon = UIImageView()
icon.image = UIImage(named: "qqicon")
icon.sizeToFit()
icon.frame.origin = CGPoint(x: 100, y: 100)
icon.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(iconTap))
icon.addGestureRecognizer(tap)
view.addSubview(icon)
//发射位置
emitterLayer.emitterPosition = icon.center
//发射源的大小
emitterLayer.emitterSize = CGSize(width: 40, height: 40)
//发射源的形状
emitterLayer.emitterShape = kCAEmitterLayerCircle
//发射模式
emitterLayer.emitterMode = kCAEmitterLayerOutline
view.layer.addSublayer(emitterLayer)
let emitterCell = CAEmitterCell()
emitterCell.name = "emitterButton"
//设置粒子的速度
emitterCell.velocity = 50
//设置粒子的速度范围
emitterCell.velocityRange = 50
//设置粒子参数的速度乘数因子
emitterCell.birthRate = 0
//设置粒子的生命周期
emitterCell.lifetime = 1
//设置粒子透明度的改变速度
emitterCell.alphaSpeed = -1
//设置要展现的图片
emitterCell.contents = UIImage(named: "fire.png")!.cgImage
emitterLayer.emitterCells = [emitterCell]
}
@objc func iconTap() {
print(7777)
UIView.animate(withDuration: 0.5) {
let basic = CABasicAnimation(keyPath: "emitterCells.emitterButton.birthRate")
basic.fromValue = 100
basic.toValue = 0
basic.duration = 0
basic.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
self.emitterLayer.add(basic, forKey: nil)
}
}
效果图.gif
火焰
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black
let emitterCell = CAEmitterCell()
emitterCell.name = "fire"
emitterCell.emissionLongitude = CGFloat.pi// emissionLongitude:x-y 平面的 发 射方向
emitterCell.velocity = -10// 粒子速度 负数表明向上燃烧
emitterCell.velocityRange = 50// 粒子速度范围
emitterCell.emissionRange = 1.1//周围发射角度
emitterCell.yAcceleration = -200// 粒子y方向的加速度分量
emitterCell.scaleSpeed = 0.2// 缩放比例 超大火苗
emitterCell.color = UIColor(red: 0.7 ,green: 0.4 ,blue:0.2 ,alpha:0.1).cgColor
emitterCell.contents = UIImage(named: "fire.png")!.cgImage
let emitterLayer = CAEmitterLayer()
emitterLayer.position = self.view.center// 粒子发射位置
emitterLayer.emitterSize = CGSize(width: 50, height: 10)// 控制火苗大小
emitterLayer.renderMode = kCAEmitterLayerAdditive
emitterLayer.emitterMode = kCAEmitterLayerOutline// 控制发射源模式 即形状
emitterLayer.emitterShape = kCAEmitterLayerLine
emitterLayer.emitterCells = [emitterCell]
self.view.layer.addSublayer(emitterLayer)
emitterLayer.setValue(500, forKeyPath: "emitterCells.fire.birthRate")
emitterLayer.setValue(1, forKeyPath: "emitterCells.fire.lifetime")
}
}
效果图.gif
霓虹效果
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black
let emitterCell = CAEmitterCell()
emitterCell.name = "nenolight"
emitterCell.emissionLongitude = CGFloat.pi * 2// emissionLongitude:x-y 平面的 发 射方向
emitterCell.velocity = 50// 粒子速度
emitterCell.velocityRange = 50// 粒子速度范围
emitterCell.scaleSpeed = -0.2// 缩放比例 超大火苗
emitterCell.scale = 0.1
emitterCell.greenSpeed = -0.1
emitterCell.redSpeed = -0.2
emitterCell.blueSpeed = 0.1
emitterCell.alphaSpeed = -0.2
emitterCell.birthRate = 100
emitterCell.lifetime = 4
emitterCell.color = UIColor.white.cgColor
emitterCell.contents = UIImage(named: "neonlight.png")!.cgImage
let emitterLayer = CAEmitterLayer()
emitterLayer.position = self.view.center// 粒子发射位置
emitterLayer.emitterSize = CGSize(width: 2, height: 2)// 控制粒子大小
emitterLayer.renderMode = kCAEmitterLayerBackToFront
emitterLayer.emitterMode = kCAEmitterLayerOutline// 控制发射源模式 即形状
emitterLayer.emitterShape = kCAEmitterLayerCircle
emitterLayer.emitterCells = [emitterCell]
self.view.layer.addSublayer(emitterLayer)
}
}
效果图.gif
网友评论