CAEmitterLayer emits random unwanted particles on touch events

ぐ巨炮叔叔 提交于 2019-12-02 19:56:29
Gabriel Pires

I know this is an old post, but I also had this problem. Jackslash answers it well in this post: iOS 7 CAEmitterLayer spawning particles inappropriately

You need to set beginTime on your emitter layer to begin at the current time with CACurrentMediaTime(). It seems the problem we have occurs because the emitter started already in the past.

emitter.beginTime = CACurrentMediaTime();

Could it be that you aren't checking to see if the particle is emitting like in the Wenderlich example Artur Ozieranski posted? I'm not seeing the doubling as long as the check is in place.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [fireView setEmitterPositionFromTouch: [touches anyObject]];
    [fireView setIsEmitting:YES];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [fireView setIsEmitting:NO];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [fireView setIsEmitting:NO];
}
-(void)setIsEmitting:(BOOL)isEmitting
{
    //turn on/off the emitting of particles
    [fireEmitter setValue:[NSNumber numberWithInt:isEmitting?200:0] forKeyPath:@"emitterCells.fire.birthRate"];
}

.h file

 #import <UIKit/UIKit.h>

    @interface DWFParticleView : UIView

    -(void)setEmitterPositionFromTouch: (CGPoint*)t;
    -(void)setIsEmitting:(BOOL)isEmitting;

    @end

.m file

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

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

    -(void)awakeFromNib
    {
        //set ref to the layer
        fireEmitter = (CAEmitterLayer*)self.layer; //2
        //configure the emitter layer
        fireEmitter.emitterPosition = CGPointMake(50, 50);
        fireEmitter.emitterSize = CGSizeMake(10, 10);

        CAEmitterCell* fire = [CAEmitterCell emitterCell];
        fire.birthRate = 0;
        fire.lifetime = 1.5;
        fire.lifetimeRange = 0.3;
        fire.color = [[UIColor colorWithRed:255 green:255 blue:255 alpha:0.1] CGColor];
        fire.contents = (id)[[UIImage imageNamed:@"Particles_fire.png"] CGImage];
        [fire setName:@"fire"];

        fire.velocity =5;
        fire.velocityRange = 20;
        fire.emissionRange = M_PI_2;

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

        fireEmitter.renderMode = kCAEmitterLayerAdditive;

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

    }

    + (Class) layerClass //3
    {
        //configure the UIView to have emitter layer
        return [CAEmitterLayer class];
    }

    -(void)setEmitterPositionFromTouch: (CGPoint*)t
    {
        //change the emitter's position
        fireEmitter.emitterPosition = (*t);
    }

    -(void)setIsEmitting:(BOOL)isEmitting
    {
        //turn on/off the emitting of particles
        [fireEmitter setValue:[NSNumber numberWithInt:isEmitting?100:0] forKeyPath:@"emitterCells.fire.birthRate"];
    }


    @end

I used this code for create a custom view and to emit particles on touch

Here is the call statement for emission of particle on touch

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint p =  [[touches anyObject] locationInView:self.view];
     [fireView setEmitterPositionFromTouch: &p];
     [fireView setIsEmitting:YES];

}

may be it will work for you .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!