CAEmitterLayer在触摸事件中发出随机不需要的粒子

我试图build立一个CAEmitterLayer来创造一个五彩纸屑的效果,我遇到了两个问题:

  1. 每当我将我的细胞上的birthRate设置为非零值以启动animation时,我会在屏幕上随机放置一组细胞,这会正常生成animation,然后发射器会继续正确地发射。
  2. 每当emitterCells在屏幕上绘图的时候,无论我什么时候触摸屏幕,发射器emitterCells在随机的(看似)随机的时间内(看起来)随机的位置绘制emitterCells 。 发射器中没有任何东西与任何触摸事件绑定(即,我不是故意在触摸事件上绘制任何东西),但该图层位于具有多个embedded视图的视图中。 我越接触,越多的细胞出现。

这里是我的代码,用于设置发射器,然后启动和停止(一旦我调用停止函数,然后点击屏幕停止创build新的随机元素):

 - (void)setupConfetti { self.confettiLayer = [CAEmitterLayer layer]; [self.view.layer addSublayer:self.confettiLayer]; [self.view.layer setNeedsDisplay]; self.confettiLayer.emitterPosition = CGPointMake(1024.0/2,-50.0); self.confettiLayer.emitterSize = CGSizeMake(1000.0, 10.0); self.confettiLayer.emitterShape = kCAEmitterLayerLine; self.confettiLayer.renderMode =kCAEmitterLayerUnordered; CAEmitterCell *confetti = [CAEmitterCell emitterCell]; confetti1.contents = (id)[[UIImage imageNamed:@"confetti.png"] CGImage]; confetti.emissionLongitude = M_PI; confetti.emissionLatitude = 0; confetti.lifetime = 5; confetti.birthRate = 0.0; confetti.velocity = 125; confetti.velocityRange = 50; confetti.yAcceleration = 50; confetti.spin = 0.0; confetti.spinRange = 10; confetti.name = @"confetti1"; self.confettiLayer.emitterCells = [NSArray arrayWithObjects:confetti, nil]; } 

开始五彩纸屑:

 - (void)startConfettiAnimation { [self.confettiLayer setValue:[NSNumber numberWithInt:10.0] forKeyPath:@"emitterCells.confetti.birthRate"]; } 

并阻止它:

 - (void)stopConfettiAnimation { [self.confettiLayer setValue:[NSNumber numberWithInt:0.0] forKeyPath:@"emitterCells.confetti.birthRate"]; } 

再次,一旦开始,随机元素的初始乱舞后,这工作得很好:一切animation正常,而当birthRate后来设置为零,它优雅地结束。 它似乎只是对触摸事件做出反应,我不知道为什么。 我已经尝试添加emitterLayer到不同的视图,禁用该视图上的用户交互,然后将其添加为主视图的子视图,似乎并没有工作。

任何帮助/见解将非常感谢!

谢谢,山姆

我知道这是一个旧的post,但我也有这个问题。 Jackslash在这篇文章中回答得很好: iOS 7 CAEmitterLayer不适当地产生粒子

您需要在发射器层上设置beginTime,以使用CACurrentMediaTime()在当前时间开始。 看来我们已经发生了这个问题,因为排放者已经开始了。

 emitter.beginTime = CACurrentMediaTime(); 

难道是你没有检查,看看是否像Wenderlich例子Artur Ozieranski发布的粒子发射? 只要支票已经到位,我不会看到翻倍。

 - (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文件

  #import <UIKit/UIKit.h> @interface DWFParticleView : UIView -(void)setEmitterPositionFromTouch: (CGPoint*)t; -(void)setIsEmitting:(BOOL)isEmitting; @end 

.m文件

  #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 

我使用这个代码来创build一个自定义的视图,并触发粒子

这是触发粒子发射的通话声明

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

可能会对你有用。

Interesting Posts