Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27191

iOS CAEmitterCell firework effect

I'm wanna create an effect like firework. I know that I can use for it CAEmitterCell. I tried to download few examples, but I still in progress with this feature in iOS 5.0

Anybody know good tutorial how to create firework effect using CAEmitterCell?

I need to create something like below:

enter image description here

All particles are going from the center (red circle) in direction to green circles. Red circle this is initial point that will have some CGPoint value.

Upvotes: 3

Views: 3892

Answers (1)

Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27191

I have found this solution:

it's how DWFParticleView.h file looks

#import <UIKit/UIKit.h>

@interface DWFParticleView : UIView

@property (nonatomic) CGFloat time;

-(void)configurateEmmiter;
-(void)setEmitterPositionFromTouch: (UITouch*)t;
-(void)setIsEmitting:(BOOL)isEmitting andPoint:(CGPoint)point;

@end

this is how looks DWFParticleView.m

#import "DWFParticleView.h"
#import <QuartzCore/QuartzCore.h>

@implementation DWFParticleView
{
    CAEmitterLayer* fireEmitter; //1
}

-(void)configurateEmmiter
{
    //set ref to the layer
    fireEmitter = (CAEmitterLayer*)self.layer; //2

    //configure the emitter layer
    fireEmitter.emitterPosition = CGPointMake(50, 50);
    fireEmitter.emitterSize = CGSizeMake(5, 5);

    CAEmitterCell* fire = [CAEmitterCell emitterCell];
    fire.birthRate = 0;
    fire.lifetime = 2.0;
    fire.lifetimeRange = 0.5;
    //fire.color = [[UIColor colorWithRed:0.8 green:0.4 blue:0.2 alpha:0.6] CGColor];
    fire.contents = (id)[[UIImage imageNamed:@"star_icon.png"] CGImage];
    [fire setName:@"fire"];


    fire.velocity = 80;
    fire.velocityRange = 20;
    fire.emissionRange = M_PI * 2.0f;

    fire.scaleSpeed = 0.1;
    fire.spin = 0.5;

    //add the cell to the layer and we're done
    fireEmitter.emitterCells = [NSArray arrayWithObject:fire];

    fireEmitter.renderMode = kCAEmitterLayerAdditive;

}

+ (Class) layerClass {
    return [CAEmitterLayer class];
}

-(void)setEmitterPositionFromTouch: (UITouch*)t
{
    //change the emitter's position
    fireEmitter.emitterPosition = [t locationInView:self];
}

-(void)setIsEmitting:(BOOL)isEmitting andPoint:(CGPoint)point
{

    fireEmitter.emitterPosition = point;

    //turn on/off the emitting of particles
    [fireEmitter setValue:[NSNumber numberWithInt:isEmitting?50:0]
               forKeyPath:@"emitterCells.fire.birthRate"];


    [self performSelector:@selector(decayStep) withObject:nil afterDelay:self.time];
}

- (void)decayStep {
    [fireEmitter setValue:[NSNumber numberWithInt:0]
               forKeyPath:@"emitterCells.fire.birthRate"];
}

@end

this is method one tap on screen for example that shows our effect

- (void)tap {
    DWFParticleView *fireView = [[DWFParticleView alloc] init];
    fireView.time = 0.3;
    [fireView setUserInteractionEnabled:NO];
    [fireView configurateEmmiter];
    [fireView setFrame:CGRectMake(0, 0, 0, 0)];
    [fireView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:fireView];
}

the material I have found here: ray

Upvotes: 5

Related Questions