CAEmitterCell不尊重birthRate的变化

时间:2013-05-25 12:12:18

标签: ios caemitterlayer caemittercell

我想创建一个只在用户触摸屏幕时发出的粒子效果,但是一旦设置为非零值,我就无法更改CAEmitterCell birthRate属性。

我有一个UIView的子类,它按照我想要的方式设置我的CAEmitterLayer和我的CAEmitterCell。我在该类上定义了两个属性:

@property (strong, nonatomic) CAEmitterLayer *emitterLayer;
@property (strong, nonatomic) CAEmitterCell *emitterCell;

然后,在我的视图控制器中,我正在跟踪触摸,设置emitterLayer的位置和emitterCell的出生率:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint tappedPt = [touch locationInView:touch.view];
    NSLog(@"began x:%f y:%f",tappedPt.x, tappedPt.y);
    emitterView.emitterCell.birthRate = 42;
    emitterView.emitterLayer.emitterPosition = tappedPt;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint tappedPt = [touch locationInView:touch.view];
    NSLog(@"moved x:%f y:%f",tappedPt.x, tappedPt.y);
    emitterView.emitterLayer.emitterPosition = tappedPt;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"ending %f", emitterView.emitterCell.birthRate);
    emitterView.emitterCell.birthRate = 0.00;
    NSLog(@"ended %f", emitterView.emitterCell.birthRate);
}

日志报告emitterView.emitterCell.birthRate发生了变化:

began x:402.000000 y:398.500000
ending 42.000000
ended 0.000000

当我触摸屏幕时,发射器按预期启动,图层跟随触摸,但是当我结束触摸时,发射器单元愉快地发出最初设置的任何值(touchesBegan中设置的值)。无论我做什么,一旦设置为非零值,我似乎无法改变出生率值。日志报告值已正确设置,但发射器不断发光。

但是,如果我更改touchesEnded方法来更改图层的位置,在我在emitterCell上设置birthRate后,一切都按预期工作:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint tappedPt = [touch locationInView:touch.view];
    NSLog(@"began x:%f y:%f",tappedPt.x, tappedPt.y);

    NSLog(@"ending %f", emitterView.emitterCell.birthRate);
    emitterView.emitterCell.birthRate = 0.0;
    NSLog(@"ended %f", emitterView.emitterCell.birthRate);
    emitterView.emitterLayer.emitterPosition = tappedPt;
}

有人可以解释一下原因吗?

2 个答案:

答案 0 :(得分:6)

我不知道为什么它表现得有些奇怪但我发现使用键值编码解决了问题并且单元格停止发射:

假设你的CAEmitterLayer是“yourEmitterLayer”并且你有一个名为“yourEmitterCell”的CAEmitterCell,如果放在touchesEnded中,这将停止发射:

[yourEmitterLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"emitterCells.yourEmitterCell.birthRate"];

答案 1 :(得分:4)

要停止发射粒子,您必须将birthRate实例的CAEmitterLayer属性设置为0,尽管它最初设置在CAEmitterCell实例上...不确定原因,但它有效

Swift 3示例:

func emitParticles() {
    let particlesEmitter = CAEmitterLayer()
    particlesEmitter.emitterPosition = center
    particlesEmitter.emitterShape = kCAEmitterLayerCircle
    particlesEmitter.emitterSize = CGSize(width: 50, height: 50)
    particlesEmitter.renderMode = kCAEmitterLayerAdditive

    let cell = CAEmitterCell()
    cell.birthRate = 15
    cell.lifetime = 1.0
    cell.color = bubble.color.cgColor
    cell.velocity = 150
    cell.velocityRange = 50
    cell.emissionRange = .pi
    cell.scale = 0.1
    cell.scaleSpeed = -0.1

    cell.contents = UIImage(named: "particle")?.cgImage
    particlesEmitter.emitterCells = [cell]

    layer.addSublayer(particlesEmitter)

    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        particlesEmitter.birthRate = 0
    }
}