【问题标题】:iOS CAEmitterCell firework effectiOS CAEmitterCell 烟花效果
【发布时间】:2012-11-08 18:22:26
【问题描述】:

我想创造一个像烟花一样的效果。我知道我可以使用 CAEmitterCell。我尝试下载几个示例,但我仍在 iOS 5.0 中使用此功能

有人知道如何使用CAEmitterCell制作烟花效果的好教程吗?

我需要创建如下内容:

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

【问题讨论】:

    标签: ios ios5


    【解决方案1】:

    我找到了这个解决方案:

    这是 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

    【讨论】:

    • 有谁知道 iOS CAEmitterCell 是否存在粒子生成器辅助程序?
    • 你能用额外的信息描述你的问题吗?你需要什么?
    • 查看 Particle Playground vigorouscoding.com/mac-apps/particle-playground 它可以让您在 Mac 上建模,然后移植到 iOS
    猜你喜欢
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多