CAEmitterLayer 和 CAEmitterCell是完成粒子动画的两个类,上代码演示一下大概怎么用的:
- (void)addEmitter {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height)];
imageView.image = [UIImage imageNamed:@"huajiTree"];
imageView.contentMode = UIViewContentModeScaleToFill;
[self.view addSubview:imageView];
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
//例子发射位置
emitterLayer.emitterPosition = CGPointMake(self.view.center.x, 120);
//发射源的尺寸大小
emitterLayer.emitterSize = CGSizeMake(100,1);
//发射模式
emitterLayer.emitterMode = kCAEmitterLayerSurface;
//发射源的形状
emitterLayer.emitterShape = kCAEmitterLayerLine;
//创建粒子
CAEmitterCell *huajiCell = [CAEmitterCell emitterCell];
//粒子的名字
huajiCell.name = @"huaji";
//粒子参数的速度乘数因子
huajiCell.birthRate = 3.0;
huajiCell.lifetime = 20.0;
//粒子速度
huajiCell.velocity =10.0;
//粒子的速度范围
huajiCell.velocityRange = 10;
//粒子y方向的加速度分量
huajiCell.yAcceleration = 5;
//周围发射角度
huajiCell.emissionRange = 0.8 * M_PI;
//子旋转角度范围
huajiCell.spinRange = 0.1 * M_PI;
huajiCell.contents = (id)[[UIImage imageNamed:@"image_emoticon25"] CGImage];
emitterLayer.emitterCells = [NSArray arrayWithObjects:huajiCell,nil];
[imageView.layer insertSublayer:emitterLayer atIndex:0];
}
如果要从一个点发射,就改成这样:
//发射源的尺寸大小
emitterLayer.emitterSize = CGSizeMake(1,1);
//发射源的形状
emitterLayer.emitterShape = kCAEmitterLayerPoint;
Demo地址,附上效果图:手动滑稽