2012-11-01 33 views
3

我想创建一个像烟火效果。我知道我可以使用它CAEmitterCell。我试图下载几个例子,但我仍然在iOS 5.0中使用此功能进行中iOS CAEmitterCell烟花效果

任何人都知道好教程如何使用CAEmitterCell创建烟花效果?

我需要象下面这样创造的东西:

enter image description here

所有的粒子都从中心(红圈)的方向,以绿色兜兜。红色圆圈这是初始点,将有一些CGPoint值。

回答

5

我发现这个解决方案:

它看起来文件如何DWFParticleView.h

#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 

这是怎么看起来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 

这个方法在屏幕上一个水龙头举例说明我们的效果

- (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]; 
} 

我在这里找到的材料:ray

+0

有谁知道iOS CAEmitterCell是否存在粒子生成器辅助程序? – Ospho

+0

你能用额外的信息来描述你的问题吗?你需要什么? –

+1

查看粒子游乐场http://www.vigorouscoding.com/mac-apps/particle-playground/它可以让你模拟你的Mac上的东西,然后移植到iOS – amergin