Question

I'm simply adding a UIView to my storyboard view controller and then creating a UIView class to display a particle effect. I change the UIView's class name to that of the custom class I created. The code I'm using in the UIView class does not display the particle effect I expected. The code is as follows:

- (void)drawRect:(CGRect)rect
{
    // Drawing code

        CGRect viewBounds = rect;

    fireworksEmitter.emitterPosition = CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height);
    fireworksEmitter.emitterSize    = CGSizeMake(viewBounds.size.width/2.0, 0.0);
    fireworksEmitter.emitterMode    = kCAEmitterLayerOutline;
    fireworksEmitter.emitterShape   = kCAEmitterLayerLine;
    fireworksEmitter.renderMode     = kCAEmitterLayerAdditive;
    fireworksEmitter.seed = (arc4random()%100)+1;

    // Create the rocket
    CAEmitterCell* rocket = [CAEmitterCell emitterCell];

    rocket.birthRate        = 1.0;
    rocket.emissionRange    = 0.25 * M_PI;  // some variation in angle
    rocket.velocity         = 380;
    rocket.velocityRange    = 100;
    rocket.yAcceleration    = 75;
    rocket.lifetime         = 1.02; // we cannot set the birthrate < 1.0 for the burst

    rocket.contents         = (id) [[UIImage imageNamed:@"DazRing"] CGImage];
    rocket.scale            = 0.2;
    rocket.color            = [[UIColor redColor] CGColor];
    rocket.greenRange       = 1.0;      // different colors
    rocket.redRange         = 1.0;
    rocket.blueRange        = 1.0;
    rocket.spinRange        = M_PI;     // slow spin



    // the burst object cannot be seen, but will spawn the sparks
    // we change the color here, since the sparks inherit its value
    CAEmitterCell* burst = [CAEmitterCell emitterCell];

    burst.birthRate         = 1.0;      // at the end of travel
    burst.velocity          = 0;
    burst.scale             = 2.5;
    burst.redSpeed          =-1.5;      // shifting
    burst.blueSpeed         =+1.5;      // shifting
    burst.greenSpeed        =+1.0;      // shifting
    burst.lifetime          = 0.35;

    // and finally, the sparks

    CAEmitterCell* spark = [CAEmitterCell emitterCell];

    spark.birthRate         = 400;
    spark.velocity          = 125;
    spark.emissionRange     = 2* M_PI;  // 360 deg
    spark.yAcceleration     = 75;       // gravity
    spark.lifetime          = 3;

    spark.contents          = (id) [[UIImage imageNamed:@"DazStarOutline"] CGImage];
    spark.scaleSpeed        =-0.2;
    spark.greenSpeed        =-0.1;
    spark.redSpeed          = 0.4;
    spark.blueSpeed         =-0.1;
    spark.alphaSpeed        =-0.25;
    spark.spin              = 2* M_PI;
    spark.spinRange         = 2* M_PI;

    // putting it together
    fireworksEmitter.emitterCells   = [NSArray arrayWithObject:rocket];
    rocket.emitterCells             = [NSArray arrayWithObject:burst];
    burst.emitterCells              = [NSArray arrayWithObject:spark];
    [self.layer addSublayer:fireworksEmitter];

    [self setNeedsDisplay];
}
Was it helpful?

Solution

1) Remove all of this code from drawRect, and put it into the UIView subclass init method, or, since you're using storyboard, put it into awakeFromNib:

- (void)awakeFromNib {
    [super awakeFromNib];

    CGRect viewBounds = rect;

    fireworksEmitter = (CAEmitterLayer*)self.layer;
    fireworksEmitter.emitterPosition = CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height);
    fireworksEmitter.emitterSize    = CGSizeMake(viewBounds.size.width/2.0, 0.0);
    fireworksEmitter.emitterMode    = kCAEmitterLayerOutline;
    fireworksEmitter.emitterShape   = kCAEmitterLayerLine;
    fireworksEmitter.renderMode     = kCAEmitterLayerAdditive;
    fireworksEmitter.seed = (arc4random()%100)+1;

    // Create the rocket
    CAEmitterCell* rocket = [CAEmitterCell emitterCell];

    rocket.birthRate        = 1.0;
    rocket.emissionRange    = 0.25 * M_PI;  // some variation in angle
    rocket.velocity         = 380;
    rocket.velocityRange    = 100;
    rocket.yAcceleration    = 75;
    rocket.lifetime         = 1.02; // we cannot set the birthrate < 1.0 for the burst

    rocket.contents         = (id) [[UIImage imageNamed:@"DazRing"] CGImage];
    rocket.scale            = 0.2;
    rocket.color            = [[UIColor redColor] CGColor];
    rocket.greenRange       = 1.0;      // different colors
    rocket.redRange         = 1.0;
    rocket.blueRange        = 1.0;
    rocket.spinRange        = M_PI;     // slow spin

    CAEmitterCell* burst = [CAEmitterCell emitterCell];
    burst.birthRate         = 1.0;      // at the end of travel
    burst.velocity          = 0;
    burst.scale             = 2.5;
    burst.redSpeed          =-1.5;      // shifting
    burst.blueSpeed         =+1.5;      // shifting
    burst.greenSpeed        =+1.0;      // shifting
    burst.lifetime          = 0.35;

    CAEmitterCell* spark = [CAEmitterCell emitterCell];
    spark.birthRate         = 400;
    spark.velocity          = 125;
    spark.emissionRange     = 2* M_PI;  // 360 deg
    spark.yAcceleration     = 75;       // gravity
    spark.lifetime          = 3;

    spark.contents          = (id) [[UIImage imageNamed:@"DazStarOutline"] CGImage];
    spark.scaleSpeed        =-0.2;
    spark.greenSpeed        =-0.1;
    spark.redSpeed          = 0.4;
    spark.blueSpeed         =-0.1;
    spark.alphaSpeed        =-0.25;
    spark.spin              = 2* M_PI;
    spark.spinRange         = 2* M_PI;

    fireworksEmitter.emitterCells   = [NSArray arrayWithObject:rocket];
    rocket.emitterCells             = [NSArray arrayWithObject:burst];
    burst.emitterCells              = [NSArray arrayWithObject:spark];
}

OTHER TIPS

Your code looks fine, so here are a few suggestions

  • you don't show where you are creating fireworksEmitter. If you do not alloc and init it somewhere, you will get a blank screen with no errors or warnings.

  • if you do not have (alpha-channel) assets named "DazRing" and "DazStarOutline" (check spelling), you will get a blank screen with no errors or warnings

Although this code will run from inside drawRect, this is not an appropriate place for it. You are not doing any drawing (directly) - rather you are setting up and adding a layer. And you certainly shouldn't be calling setNeedsDisplay from inside drawRect. This code will work just as well or better if you put it (for example) in awakeFromNib.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top