我正在玩 CAEmitterLayer,现在我遇到了一些问题:(
例如,我需要一个短粒子效果——比如撞击或爆炸——在一个地方(所以我在这个地方有一个小的 UIView)。我该怎么做?
1,我有一个想法——用它的粒子创建emitterLayer并将lifeTime设置为0。例如,当我需要它时,我将lifeTime设置为1,过了一会儿我可以将它设置回0。 - 但它没有做任何事情:(
2、第二个想法是每次需要时都创建[CAEmitterLayer layer]并将其添加为layers sublayer。但是我在想当我重复它十次时会发生什么……我有 10 个子层,其中一个活跃,9 个“死”?一般如何停止发射?一段时间后我有 performSelector 将生命周期设置为 0 和其他具有更长间隔的选择器来 removeFromSuperlayer ......但它并不像我想要的那样漂亮:( 还有另一种“正确”的方式吗?
我认为子层太多与我的另一个问题有关……我只想发射一个粒子。当我这样做时,它会起作用。但有时它会发出三个粒子,有时是两个……这让我很生气。当我不停止发射器时,它每次都会给出正确数量的粒子......
所以问题...</p>
如何在短时间内发射粒子。如何使用它们 - 比如停止,从父层移除,......如何发射精确数量的粒子
编辑:
emitter = [CAEmitterLayer layer];
emitter.emitterPosition = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
emitter.emitterMode = kCAEmitterLayerPoints;
emitter.emitterShape = kCAEmitterLayerPoint;
emitter.renderMode = kCAEmitterLayerOldestFirst;
emitter.lifetime = 0;
particle = [CAEmitterCell emitterCell];
[particle setName:@"hit"];
particle.birthRate = 1;
particle.emissionLongitude = 3*M_PI_2;//270deg
particle.lifetime = 0.75;
particle.lifetimeRange = 0;
particle.velocity = 110;
particle.velocityRange = 20;
particle.emissionRange = M_PI_2;//PI/2 = 90degrees
particle.yAcceleration = 200;
particle.contents = (id) [[UIImage imageNamed:@"50"] CGImage];
particle.scale = 1.0;
particle.scaleSpeed = -0.5;
particle.alphaSpeed = -1.0;
emitter.emitterCells = [NSArray arrayWithObject:particle];
[(CAEmitterLayer *)self.view.layer addSublayer: emitter];
然后在与按钮链接的方法中,我这样做:
emitter.lifetime = 1.0;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.9 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
emitter.lifetime = 0;
});
更改为@David Rönnqvist 态度后已编辑和更新
CAEmitterCell *dustCell = [CAEmitterCell emitterCell];
[dustCell setBirthRate:1];
[dustCell setLifetime:1.5];
[dustCell setName:@"dust"];
[dustCell setContents:(id) [[UIImage imageNamed:@"smoke"] CGImage]];
[dustCell setVelocity:50];
[dustCell setEmissionRange:M_PI];
// Various configurations for the appearance...
// This is the only cell with configured scale,
// color, content, emissionLongitude, etc...
[emitter setEmitterCells:[NSArray arrayWithObject:dustCell]];
[(CAEmitterLayer *)self.view.layer addSublayer:emitter];
// After one burst, change the birth rate of the cloud to 0
// so that there is only one burst per side.
double delayInSeconds = 0.5; // One cloud will have been created by now, but not two
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
[emitter setLifetime:0.0];
[emitter setValue:[NSNumber numberWithFloat:0.0]
forKeyPath:@"emitterCells.dust.birthRate"];
});