CAEmitterCell trail

Discussion in 'iOS Programming' started by moonman239, Nov 19, 2014.

  1. moonman239, Nov 19, 2014
    Last edited: Nov 19, 2014

    moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    So I have decided to build my star burst animation from the ground up using Core Animation. I have a bunch of little stars, created using a CAEmitterCell, that come out of this bigger star. What I want to do is have each little star leave a trail as it goes along. I know I can draw a CGPath on a UIView.

    Edit: Found my answer: Just create another emitter cell for the trail, and add it to the existingCells property of the first emitter cell.
     
  2. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #2
    I got it working (sort of). What I need to do now is make it so that the first emitter cell's emitter emits the trail particles so that the trail particles don't go over the star. I'm thinking I can do this by setting the first emitter cell's emitterLatitude and/or emitterLongitude properties.

    Here's my code:
    Code:
        CALayer *viewLayer = [self.view layer];
        // Create a particle emission layer and set its properties.
        layer = [CAEmitterLayer layer];
        NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"star copy" ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        id cgimage = (__bridge id)[image CGImage];
        [[layer contents] addObject:cgimage];
        CGRect rect = viewLayer.frame;
        [layer setFrame:rect];
        layer.emitterPosition = layer.position; //
        layer.emitterSize = [image size];
        layer.emitterShape = kCAEmitterLayerCircle;
        layer.lifetime = 3;
        // Create an emitter cell for the little stars.
        CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
        emitterCell.contents = cgimage;
        emitterCell.birthRate = 4;
        emitterCell.lifetime = 2;
        emitterCell.lifetimeRange = 1;
        emitterCell.velocity = 300;
        emitterCell.velocityRange = 20;
        emitterCell.emissionRange = M_PI * 2.0f;
        // Create the trail.
        CAEmitterCell *trailEmitterCell = [CAEmitterCell emitterCell];
        trailEmitterCell.velocity = 0;
        trailEmitterCell.birthRate = 50;
        trailEmitterCell.lifetime = 0.2;
        trailEmitterCell.emissionRange = 1.0f;
        NSString *trailImagePath = [[NSBundle mainBundle] pathForResource:@"r" ofType:@"png"];
        id trailImage = (__bridge id)[[UIImage imageWithContentsOfFile:trailImagePath] CGImage];
        trailEmitterCell.contents = trailImage;
        [emitterCell setEmitterCells:[NSArray arrayWithObject:trailEmitterCell]];
        layer.emitterCells = [NSArray arrayWithObject:emitterCell];
        [viewLayer addSublayer:layer];
    
     

Share This Page