上篇介紹了CAEmitterLayer的屬性和CAEmitterCell的屬性,還有CAEmitterLayer的基本用法。
這里將使用CAEmitterLayer來實(shí)現(xiàn)放煙花效果板鬓,附帶尾焰效果艺晴。先看效果圖
煙花效果.gif
GitHub工程項(xiàng)目地址在CAAnimation_CAEmitterLayer
工程里面的FireworksViewController
里
先上代碼,需要注意的問題會(huì)在代碼后面說明:
//MARK: - 第二種煙花效果
extension FireworksViewController{
/**
* 注意事項(xiàng):
* 1屿良、CAEmitterCell也是可以設(shè)置emitterCells屬性悠栓,表示的是粒子的粒子束
* 2霉涨、設(shè)置CAEmitterCell的emitterCells屬性時(shí),emitterCells的是在cell出現(xiàn)之后再出現(xiàn)的
* 3惭适、設(shè)置birthRate=1的時(shí)候笙瑟,cell出現(xiàn)的時(shí)間點(diǎn)并不是在一秒時(shí)間剛好到的節(jié)點(diǎn),而是稍有延遲癞志,在1.0-1.02之間(目前理解是這樣)
* 4往枷、要想設(shè)置粒子的粒子束,首先要掌握好時(shí)間節(jié)點(diǎn)
*/
//初始化一個(gè)粒子layer
private func setUpFireworksEmitterLayer(){
let bottomLayer = CAEmitterLayer()
//設(shè)置發(fā)射源樣式
bottomLayer.emitterMode = kCAEmitterLayerPoint
//設(shè)置發(fā)射模式
bottomLayer.emitterShape = kCAEmitterLayerLine
//設(shè)置發(fā)射源位置
bottomLayer.emitterPosition = CGPoint(x: 0, y: view.bounds.height-20)
let lineCell = CAEmitterCell()
//設(shè)置個(gè)數(shù)
lineCell.birthRate = 1
//設(shè)置contents
lineCell.contents = #imageLiteral(resourceName: "Heart_red_02").cgImage
//設(shè)置scale
lineCell.scale = 0.5
//設(shè)置發(fā)射速度
lineCell.velocity = 350
//設(shè)置速度浮動(dòng)值
lineCell.velocityRange = 200
//設(shè)置發(fā)射角度
lineCell.emissionLongitude = CGFloat.pi/2
//設(shè)置時(shí)長
lineCell.lifetime = 1.02
//設(shè)置尾巴cell
let tailCell = CAEmitterCell()
//設(shè)置個(gè)數(shù)
tailCell.birthRate = 200
//設(shè)置contents
tailCell.contents = #imageLiteral(resourceName: "Heart_red_02").cgImage
//設(shè)置scale
tailCell.scale = 0.05
//設(shè)置發(fā)射速度
tailCell.velocity = 100
//設(shè)置y軸分支加速度
tailCell.yAcceleration = 50
//設(shè)置發(fā)射角度
tailCell.emissionLatitude = -CGFloat.pi/2
//設(shè)置發(fā)射角度浮動(dòng)值
tailCell.emissionRange = CGFloat.pi/4
//設(shè)置時(shí)長
tailCell.lifetime = 1
//初始化向上的cell
let riseCell = CAEmitterCell()
//設(shè)置contents
riseCell.contents = #imageLiteral(resourceName: "Heart_blue").cgImage
//設(shè)置每秒生成的個(gè)數(shù)
riseCell.birthRate = 3
//設(shè)置發(fā)射速度
riseCell.velocity = 400
//設(shè)置發(fā)射速度浮動(dòng)值
riseCell.velocityRange = 200
//設(shè)置發(fā)射角度
riseCell.emissionLongitude = -CGFloat.pi/2
//設(shè)置時(shí)長
riseCell.lifetime = 1.02
//初始化一個(gè)放大的cell(過渡的cell)
let bigCell = CAEmitterCell()
//設(shè)置個(gè)數(shù)
bigCell.birthRate = 1
//設(shè)置時(shí)長
bigCell.lifetime = 0.2
//初始化一個(gè)擴(kuò)散的cell
let fireCell = CAEmitterCell()
//設(shè)置contents
fireCell.contents = #imageLiteral(resourceName: "Heart_red").cgImage
//設(shè)置每秒的粒子個(gè)數(shù)
fireCell.birthRate = 1000
//設(shè)置scale
fireCell.scale = 0.05
//設(shè)置擴(kuò)散速度
fireCell.velocity = 50
//設(shè)置發(fā)射角度
fireCell.emissionRange = CGFloat.pi * 2
//設(shè)置自旋角度
fireCell.spin = CGFloat.pi * 2
//設(shè)置自旋角度浮動(dòng)值
fireCell.spinRange = CGFloat.pi * 2
//設(shè)置時(shí)長
fireCell.lifetime = 2
//初始化一個(gè)擴(kuò)散的cell
let fireCell2 = CAEmitterCell()
//設(shè)置contents
fireCell2.contents = #imageLiteral(resourceName: "Heart_blue").cgImage
//設(shè)置每秒的粒子個(gè)數(shù)
fireCell2.birthRate = 1000
//設(shè)置scale
fireCell2.scale = 0.05
//設(shè)置擴(kuò)散速度
fireCell2.velocity = 50
//設(shè)置發(fā)射角度
fireCell2.emissionRange = CGFloat.pi * 2
//設(shè)置自旋角度
fireCell2.spin = CGFloat.pi * 2
//設(shè)置自旋角度浮動(dòng)值
fireCell2.spinRange = CGFloat.pi * 2
//設(shè)置時(shí)長
fireCell2.lifetime = 2
bottomLayer.emitterCells = [lineCell]
//設(shè)置lineCell的cells
lineCell.emitterCells = [tailCell,riseCell]
//設(shè)置riseCell的cells
riseCell.emitterCells = [bigCell,tailCell]
//設(shè)置bigCell的cells
bigCell.emitterCells = [fireCell,fireCell2]
view.layer.addSublayer(bottomLayer)
}
注意事項(xiàng):
- 1今阳、之前只介紹了
CAEmitterLayer
的emitterCells
屬性师溅,但是CAEmitterCell
也是有emitterCells
屬性的茅信。這里是實(shí)現(xiàn)效果圖動(dòng)畫的關(guān)鍵 - 2盾舌、如果有個(gè)
CAEmitterCell
類型的cell
設(shè)置cell.emitterCells=[cell1]
,cell1
也是CAEmitterCell
類型的,這里cell1
是基于cell
的蘸鲸,只有在cell
出現(xiàn)之后妖谴,cell1
才會(huì)開始出現(xiàn)。cell
的birthRate
會(huì)影響到cell1
的出現(xiàn)的粒子數(shù)酌摇。比如cell.birthRate = 0.2
,則cell1
出現(xiàn)的粒子數(shù)是cell1.birthRate
的0.2倍膝舅。 - 3、
CAEmitterCell
的birthRate
屬性表示的一秒時(shí)間出現(xiàn)的粒子數(shù)窑多。這里要注意的是birthRate=1的時(shí)候仍稀,這個(gè)時(shí)候粒子出現(xiàn)的時(shí)間點(diǎn)在1-1.02之間。(為什么會(huì)有這個(gè)延遲埂息,估計(jì)是CAEmitterLayer
的隨機(jī)性和浮動(dòng)性吧技潘。) - 4遥巴、在設(shè)置CAEmitterCell的emitterCells屬性的時(shí)候,要注意CAEmitterCell的lifeTime屬性享幽,以達(dá)到預(yù)期效果
注意的事項(xiàng)是自己理解的铲掐。如果有更好的說法,歡迎留言值桩。