【问题标题】:animating the CAEmitterCell Color property动画 CAEmitterCell Color 属性
【发布时间】:2013-04-12 02:01:32
【问题描述】:

我有一个CAEmitterCell,并且我已将其设置为特定颜色。文档说this property is animatable,我想为我游戏中的不同玩家(所有玩家一开始都选择他们的颜色)在多种不同颜色之间制作动画。

这是我设置时的EmitterCell

//
    // Emitter Cells
    //

    // 'New Emitter' cell configuration
    newEmitter = [CAEmitterCell emitterCell];
    newEmitter.scaleSpeed = 10;
    newEmitter.lifetime = 2.481715;
    newEmitter.velocity = 332.3636968085106;
    newEmitter.contents = newemitterImg;
    newEmitter.name = @"New Emitter";
    newEmitter.color = [[UIColor colorWithRed:0.50 green:0.00 blue:1.00 alpha:1.00] CGColor];
    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];

这里是我测试颜色变化的地方,在这种情况下只是在两种不同颜色之间跳跃:

-(void)changeColor {

    if (color == 0) {
        color = 1;
        NSLog(@"color = 1");

        [UIView animateWithDuration:1.5 delay:0 options:0 animations:^{
            newEmitter.color = [[UIColor colorWithRed:0.50 green:0.00 blue:1.00 alpha:1.00] CGColor];
        } completion:^(BOOL finished) {[self performSelector:@selector(changeColor) withObject:nil afterDelay:2];}];
    } else {
        color = 0;
        NSLog(@"color = 0");
        [UIView animateWithDuration:1.5 delay:0 options:0 animations:^{
            newEmitter.color = [[UIColor colorWithRed:1.00 green:0.50 blue:0.10 alpha:1.00] CGColor];
        } completion:^(BOOL finished) {[self performSelector:@selector(changeColor) withObject:nil afterDelay:2];}];
    }

}

但是,当我运行它时,颜色永远不会改变。我在这里误解了“Animatable”的性质,还是我只需要为CAEmitterCell 换一种方式?

【问题讨论】:

    标签: iphone ios caemitterlayer caemittercell


    【解决方案1】:

    事实上,CAEmitterCell 是不同的。要让动画在它们上运行,您需要采取以下步骤:

    1.为你的CAEmitterCell命名,例如:

    newEmitter.name = @"fire";

    2.通过CAEmitterLayer实例访问该发射器的动画属性:

    //Set first before doing CABasicAnimation so it sticks
    newEmitter.redSpeed = 1.0;
    
    //Access the property with this key path format: @"emitterCells.<name>.<property>"
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"emitterCells.fire.redSpeed"];
    anim.fromValue = @(0.0);
    anim.toValue = @(1.0);
    anim.duration = 1.5;
    anim.fillMode = kCAFillModeForwards;
    [emitter addAnimation:anim forKey:@"emitterAnim"];
    

    【讨论】:

    • 如何改变颜色?您的代码似乎不适用于更改颜色。
    【解决方案2】:

    试试这个:

        [newEmitter.emitterCells[0] setColor:[[UIColor yellowColor] CGColor]];
    

    【讨论】:

      【解决方案3】:

      您可以使用这些属性为发射器颜色设置动画。

      newEmitter.redSpeed = 1.0;
      newEmitter.greenSpeed = 1.0;
      newEmitter.blueSpeed = 1.0;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-26
        相关资源
        最近更新 更多