CAEmitterCell不尊重的持续时间值(CAEmitterCell does not resp

2019-08-22 20:52发布

我有我想要2秒后结束的发射器,但我不能让它停止运行!

我的代码如下:

// Emitter Layer
    //
    emitter = [CAEmitterLayer layer];
    emitter.emitterSize = CGSizeMake(20, 20);
    emitter.renderMode = @"additive";
    emitter.emitterMode = @"points";
    emitter.emitterShape = @"rectangle";
    emitter.emitterPosition = CGPointMake(310, 230);
    //emitter.duration = 2; // setting duration on the emitterLayer prevents anything from firing it seems

// Emitter Cells
    //

    // 'New Emitter' cell configuration
    newEmitter = [CAEmitterCell emitterCell];
    newEmitter.scaleSpeed = 7;
    newEmitter.lifetime = 2.481715;
    //newEmitter.duration = 2; // I would expect this to stop the emitter after 2 seconds, but it doesn't!
    newEmitter.velocity = 332.3636968085106;
    newEmitter.contents = newemitterImg;
    newEmitter.name = @"New Emitter";
newEmitter.scaleRange = 4.178236607142859;
    newEmitter.lifetimeRange = 1.6;
    newEmitter.greenRange = -2.775558e-17;
    newEmitter.birthRate = 40;
    newEmitter.emissionRange = -6.283185306666667;
    newEmitter.scale = 0;

    // Emitter Cell Chains
    //

    emitter.emitterCells = [NSArray arrayWithObjects:newEmitter, nil];

人们似乎对这个资料非常少,尽管是iOS版的一部分,约2年!

Answer 1:

你可以用一个定时器做到这一点,一个方法停止发射器产生新的粒子...

在现有的代码添加结束:

    [newEmitter setName:@"newEmitter"];
    [NSTimer scheduledTimerWithTimeInterval:2.0
                                     target:self
                                   selector:@selector(stopEmitter:)
                                   userInfo:nil
                                    repeats:NO];

然后做一个stopEmitter:方法...

    - (void) stopEmitter:(NSTimer*)timer
    {
        [emitter setValue:@0
               forKeyPath:@"emitterCells.newEmitter.birthRate"];
    }


文章来源: CAEmitterCell does not respect the duration value