CAEmitterLayer 粒子效果(发射器)

时间:2023-03-09 23:10:58
CAEmitterLayer 粒子效果(发射器)

//创建Layer

CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];

//边框

emitterLayer.borderWidth = 1.0f;

//尺寸

emitterLayer.frame = CGRectMake(100, 100, 100, 100);

emitterLayer.masksToBounds = YES;

//发射点

emitterLayer.emitterPosition = CGPointMake(0, 0);

//发射模式

emitterLayer.emitterMode = kCAEmitterLayerOutline;

//发射形状

emitterLayer.emitterShape = kCAEmitterLayerCircle;

[self.view.layer addSublayer:emitterLayer];

//创建粒子

CAEmitterCell *cell = [CAEmitterCell emitterCell];

//粒子产生率

cell.birthRate = 2.f;

//粒子生命周期

cell.lifetime = 10.f;//秒

//粒子速度值

cell.velocity = 10;

//速度变化值

cell.velocityRange = 3.f;//7--13

//y轴加速度

cell.yAcceleration = 2.f;

//发射角度

cell.emissionRange = 4.0*M_1_PI;

//粒子颜色

cell.color = [UIColor blueColor].CGColor;

//设置图片

cell.contents = (__bridge id)[UIImage imageNamed:@"snow.png"].CGImage;

emitterLayer.emitterCells = @[cell];