CAEmitterLayer可以来制作粒子发射系统,主要包含两个部分CAEmitterLayer和CAEmitterCell。
CAEmitterLayer就是粒子发射器,决定粒子发射器的形状,位置,大小等;CAEmitterCell可以设置粒子的属性,包括形状,大小,速度,加速度,存活时间等。
CAEmitterLayer的常用属性:
emitterCells
CAEmitterCell对象的数组
birthRate
粒子产生系数,默认为1.0;CAEmitterLayer也有birthRate属性,系统每秒实际产生的粒子数是这两个属性值的乘积
lifetime
粒子存活时间,默认1s
emitterPosition
发射器xy坐标轴上的位置
emitterZPosition
发射器在z坐标轴上的位置
preservesDepth
是否开启三维效果
velocity
粒子运动速度
scale
粒子缩放效果
spin
粒子自旋转速度
seed
随机数发生器
emitterSize
发射器尺寸
emitterDepth
发射器深度
emitterShape
发射器形状
kCAEmitterLayerPoint 点状
kCAEmitterLayerLine 线状
kCAEmitterLayerRectangle 矩形
kCAEmitterLayerCuboid 立方体
kCAEmitterLayerCircle 圆形
kCAEmitterLayerSphere 球形
emitterMode
发射模式
kCAEmitterLayerPoints 从顶点发出
kCAEmitterLayerOutline 从边缘发出
kCAEmitterLayerSurface 从表面发出
kCAEmitterLayerVolume 从立方体中发出
renderMode
渲染模式
kCAEmitterLayerUnordered 粒子无序出现
kCAEmitterLayerOldestFirst 先声明的粒子在上层
kCAEmitterLayerOldestLast 后声明的粒子在上层
kCAEmitterLayerBackToFront 在z轴上从后到前渲染
kCAEmitterLayerAdditive 混合粒子
CAEmitterCell常用属性:
emitterCell
初始化方法
name
粒子名字
color
粒子颜色
enabled
粒子是否可渲染
contents
粒子内容
contentsRect
渲染范围
birthRate
粒子产生速度
lifetime
生命周期
lifetimeRange
生命周期浮动范围
velocity
粒子初始运动速度
velocityRange
粒子速度浮动范围
spin
粒子旋转角度
spinrange
粒子旋转角度范围
scale
粒子缩放比例
scaleRange
粒子缩放比例范围
scaleSpeed
粒子缩放速度
alphaRange
粒子透明度变化范围
alphaSpeed
粒子透明度变化速度
redRange
粒子颜色红色的变化范围
redSpeed
粒子颜色红色的变化速度
blueRange
粒子颜色蓝色的变化范围
blueSpeed
粒子颜色蓝色的变化速度
greenRange
粒子颜色绿色的变化范围
greenSpeed
粒子颜色绿色的变化速度
xAcceleration
x轴方向的加速度分量
yAcceleration
y轴方向的加速度分量
zAcceleration
z轴方向的加速度分量
emissionRange
粒子发射角度范围
emissionLongitude
粒子在xy平面的发射角度
emissionLatitude
粒子在z轴方向的发射角度
使用CAEmitterLayer做一个从礼物盒里抛出来礼物的样式:
//添加一个礼物盒
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2-10, 600, 50, 50)];
imgView.image = [UIImage imageNamed:@"礼物盒"];
[self.view addSubview:imgView];
//1.创建一个发射器
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
emitterLayer.emitterPosition = CGPointMake(self.view.bounds.size.width/2, 600);
emitterLayer.emitterShape = kCAEmitterLayerLine;
emitterLayer.emitterMode = kCAEmitterLayerSurface;
emitterLayer.emitterSize = CGSizeMake(20, 5);
//2.创建发射的粒子
NSMutableArray *cellsArray = [NSMutableArray array];
for (int i = 1; i<5; i++) {
CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
emitterCell.birthRate = 0.5+0.1*i;
emitterCell.speed = 1.5;
emitterCell.velocity = 150;
emitterCell.velocityRange = 30;
emitterCell.lifetime = 3.5;
emitterCell.lifetimeRange = 0.5;
emitterCell.spin = 1;
emitterCell.scale = 0.8;
emitterCell.scaleRange = 0.5;
emitterCell.emissionLongitude = 0;
emitterCell.emissionRange = M_PI_4;
emitterCell.yAcceleration = 120;
emitterCell.contents = (id)[UIImage imageNamed:[NSString stringWithFormat:@"icon0%d", i]].CGImage;
[cellsArray addObject:emitterCell];
}
emitterLayer.emitterCells = cellsArray;
[self.view.layer addSublayer:emitterLayer];
创建一个点赞按钮的动画效果
CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
emitterCell.name = @"emitter";
emitterCell.alphaRange = 0.10;
emitterCell.alphaSpeed = -1.0;
emitterCell.lifetime = 0.3;
emitterCell.lifetimeRange = 0.1;
emitterCell.birthRate = 0;
emitterCell.velocity = 40.0;
emitterCell.velocityRange = 0.00;
emitterCell.scale = 0.3;
emitterCell.scaleRange = 0.1;
emitterCell.contents = (id)[UIImage imageNamed:@"点"].CGImage;
self.emitterLayer = [CAEmitterLayer layer];
self.emitterLayer.name = @"emitterLayer";
self.emitterLayer.emitterShape = kCAEmitterLayerCircle;
self.emitterLayer.emitterMode = kCAEmitterLayerOutline;
self.emitterLayer.emitterSize = CGSizeMake(20, 0);
self.emitterLayer.emitterCells = @[emitterCell];
self.emitterLayer.renderMode = kCAEmitterLayerOldestFirst;
self.emitterLayer.masksToBounds = NO;
self.emitterLayer.position = likeBtn.center;
self.emitterLayer.zPosition = -1;
[self.view.layer addSublayer:self.emitterLayer];
//处理按钮点击
- (void)handleLikeOrNotLike:(UIButton *)button {
button.selected = !button.selected;
if (button.selected) {
self.emitterLayer.beginTime = CACurrentMediaTime();
//每秒喷射粒子的个数为80个
[self.emitterLayer setValue:@1000 forKeyPath:@"emitterCells.emitter.birthRate"];
[self performSelector:@selector(explode) withObject:nil afterDelay:0.1];
}
[button setImage:[UIImage imageNamed:@"点赞"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"点赞-2"] forState:UIControlStateSelected];
}
//大量喷射
- (void)explode {
//每秒喷射的个数为2500个
[self.emitterLayer setValue:@2500 forKeyPath:@"emitterCells.emitter.birthRate"];
[self performSelector:@selector(stop) withObject:nil afterDelay:0.1];
}
//停止喷射
- (void)stop {
//每秒喷射粒子的个数为0个
[self.emitterLayer setValue:@0 forKeyPath:@"emitterCells.emitter.birthRate"];
}
参考链接:
http://blog.csdn.net/hero_wqb/article/details/50539444
http://blog.csdn.net/wang631106979/article/details/51258020