我试图建立一个CAEmitterLayer
做出纸屑效果,我碰到两个问题:
- 每当我设置的
birthRate
在我的细胞东西非零启动动画我得到随机放置在屏幕上的细胞,通常动画乱舞,然后发射器继续后正常发出。 - 每当
emitterCells
是在画面绘制的东西,任何时候我触摸屏幕,发射吸引emitterCells
中存在的时间(看似)随机的(貌似)随机位置。 在发射器没有什么是绑定到任何触摸事件(即我不是故意画上的触摸事件的任何东西),但该层是在具有多个嵌入式视图的视图。 我摸越多,细胞出现。
这是我设立的发射器,然后启动和停止它的代码(一旦我叫停止功能,然后轻敲屏幕停火创造新的随机元素):
- (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"];
}
同样,一旦它被启动,随机元素的初始乱舞之后,这个工作得很好:通常一切动画,当出生率后设置为零,它优雅地结束。 这似乎只是响应触摸事件,我不知道为什么。 我试着加入emitterLayer到不同的视图,该视图上禁用用户交互,然后将其作为主视图的子视图,而且似乎没有工作。
任何帮助/洞察力,将不胜感激!
谢谢,山姆
我知道这是旧的文章,但我也有这个问题。 Jackslash回答得好在这篇文章: iOS的7 CAEmitterLayer产卵颗粒不适当
您需要设置BEGINTIME您的发射层上在当前时间与CACurrentMediaTime开始()。 这似乎发生,我们有问题,因为发射器在过去已经开始。
emitter.beginTime = CACurrentMediaTime();
难道你不检查,看看是否粒子发射像Wenderlich例如阿图尔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
我用此代码为创建自定义视图和发射触摸颗粒
下面是关于触控颗粒排放的调用语句
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.view];
[fireView setEmitterPositionFromTouch: &p];
[fireView setIsEmitting:YES];
}
可能是它会为你工作。