一生所愛.png
前言 :整個(gè)粒子動(dòng)畫效果排霉,學(xué)習(xí)學(xué)習(xí)... 看看演示效果
lizixiaoguo.gif
Pragma mark — NO.1 創(chuàng)建最簡(jiǎn)單的粒子效果
#直接上代碼了窍株,效果都比較簡(jiǎn)單
//最簡(jiǎn)單的粒子效果
-(void)setupEmitter{
// 1.創(chuàng)建發(fā)射器
CAEmitterLayer *emitter = [[CAEmitterLayer alloc]init];
// 2.設(shè)置發(fā)射器的位置
emitter.emitterPosition = CGPointMake(self.view.center.x, self.view.bounds.size.height - 20);
// 3.開啟三維效果--可以關(guān)閉三維效果看看
emitter.preservesDepth = YES;
// 4.創(chuàng)建粒子, 并且設(shè)置粒子相關(guān)的屬性
// 4.1.創(chuàng)建粒子Cell
CAEmitterCell *cell = [[CAEmitterCell alloc]init];
// 4.2.設(shè)置粒子速度
cell.velocity = 150;
//速度范圍波動(dòng)50到250
cell.velocityRange = 100;
// 4.3.設(shè)置粒子的大小
//一般我們的粒子大小就是圖片大小, 我們一般做個(gè)縮放
cell.scale = 0.7;
//粒子大小范圍: 0.4 - 1 倍大
cell.scaleRange = 0.3;
// 4.4.設(shè)置粒子方向
//這個(gè)是設(shè)置經(jīng)度攻柠,就是豎直方向 --具體看我們下面圖片講解
//這個(gè)角度是逆時(shí)針的,所以我們的方向要么是 (2/3 π)后裸, 要么是 (-π)
cell.emissionLongitude = -M_PI_2;
cell.emissionRange = M_PI_2 / 4;
// 4.5.設(shè)置粒子的存活時(shí)間
cell.lifetime = 6;
cell.lifetimeRange = 1.5;
// 4.6.設(shè)置粒子旋轉(zhuǎn)
cell.spin = M_PI_2;
cell.spinRange = M_PI_2 / 2;
// 4.6.設(shè)置粒子每秒彈出的個(gè)數(shù)
cell.birthRate = 20;
// 4.7.設(shè)置粒子展示的圖片 --這個(gè)必須要設(shè)置為CGImage
cell.contents = (__bridge id _Nullable)([UIImage imageNamed:@"good5_30x30"].CGImage);
// 5.將粒子設(shè)置到發(fā)射器中--這個(gè)是要放個(gè)數(shù)組進(jìn)去
emitter.emitterCells = @[cell];
// 6.將發(fā)射器的layer添加到父layer中
[self.view.layer addSublayer:emitter];
}
設(shè)置粒子方向講解:cell.emissionLongitude (垂直方向) cell.emissionLatitude (水平方向)
cell.emissionLongitude.png
Pragma mark — NO.2 循環(huán)創(chuàng)建多個(gè)cell瑰钮,實(shí)現(xiàn)真正的粒子效果
//完整粒子效果
-(void)setMultipleEmitterCell{
// 1.創(chuàng)建發(fā)射器
CAEmitterLayer *emitter = [[CAEmitterLayer alloc]init];
// 2.設(shè)置發(fā)射器的位置
emitter.emitterPosition = CGPointMake(self.view.center.x, self.view.bounds.size.height - 20);
// 3.開啟三維效果
emitter.preservesDepth = YES;
//創(chuàng)建多個(gè)粒子
NSMutableArray *cellArr = [NSMutableArray array];
for (int i = 0 ; i < 9; i++) {
CAEmitterCell *cell = [[CAEmitterCell alloc]init];
cell.velocity = 150;
cell.velocityRange = 100;
cell.scale = 0.7;
cell.scaleRange = 0.3;
cell.emissionLongitude = -M_PI_2;
cell.emissionRange = M_PI_2 / 8;
cell.lifetime = 6;
cell.lifetimeRange = 1.5;
cell.spin = M_PI_2;
cell.spinRange = M_PI_2 / 2;
cell.birthRate = 2;
cell.contents = (__bridge id _Nullable)([UIImage imageNamed:[NSString stringWithFormat:@"good%d_30x30",i+1]].CGImage);
//將創(chuàng)建出來的cell加入到數(shù)組中
[cellArr addObject:cell];
}
// 5.將粒子設(shè)置到發(fā)射器中
emitter.emitterCells = cellArr;
// 6.將發(fā)射器的layer添加到父layer中
[self.view.layer addSublayer:emitter];
}
尾聲:有了這種粒子效果,在直播中可以應(yīng)用. 調(diào)整發(fā)射方向可以做頂部掉落的雪花效果等等微驶。浪谴。。
項(xiàng)目地址:粒子效果