Ios CAEmitterLayer未显示
我只是简单地将一个Ios CAEmitterLayer未显示,ios,objective-c,drawrect,caemitterlayer,caemittercell,Ios,Objective C,Drawrect,Caemitterlayer,Caemittercell,我只是简单地将一个UIView添加到我的storyboard视图控制器,然后创建一个UIView类来显示粒子效果。我将ui视图的类名更改为我创建的自定义类的名称。我在UIView类中使用的代码没有显示我期望的粒子效果。代码如下: - (void)drawRect:(CGRect)rect { // Drawing code CGRect viewBounds = rect; fireworksEmitter.emitterPosition = CGPointM
UIView
添加到我的storyboard视图控制器
,然后创建一个UIView
类来显示粒子效果。我将ui视图的
类名更改为我创建的自定义类的名称。我在UIView
类中使用的代码没有显示我期望的粒子效果。代码如下:
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect viewBounds = rect;
fireworksEmitter.emitterPosition = CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height);
fireworksEmitter.emitterSize = CGSizeMake(viewBounds.size.width/2.0, 0.0);
fireworksEmitter.emitterMode = kCAEmitterLayerOutline;
fireworksEmitter.emitterShape = kCAEmitterLayerLine;
fireworksEmitter.renderMode = kCAEmitterLayerAdditive;
fireworksEmitter.seed = (arc4random()%100)+1;
// Create the rocket
CAEmitterCell* rocket = [CAEmitterCell emitterCell];
rocket.birthRate = 1.0;
rocket.emissionRange = 0.25 * M_PI; // some variation in angle
rocket.velocity = 380;
rocket.velocityRange = 100;
rocket.yAcceleration = 75;
rocket.lifetime = 1.02; // we cannot set the birthrate < 1.0 for the burst
rocket.contents = (id) [[UIImage imageNamed:@"DazRing"] CGImage];
rocket.scale = 0.2;
rocket.color = [[UIColor redColor] CGColor];
rocket.greenRange = 1.0; // different colors
rocket.redRange = 1.0;
rocket.blueRange = 1.0;
rocket.spinRange = M_PI; // slow spin
// the burst object cannot be seen, but will spawn the sparks
// we change the color here, since the sparks inherit its value
CAEmitterCell* burst = [CAEmitterCell emitterCell];
burst.birthRate = 1.0; // at the end of travel
burst.velocity = 0;
burst.scale = 2.5;
burst.redSpeed =-1.5; // shifting
burst.blueSpeed =+1.5; // shifting
burst.greenSpeed =+1.0; // shifting
burst.lifetime = 0.35;
// and finally, the sparks
CAEmitterCell* spark = [CAEmitterCell emitterCell];
spark.birthRate = 400;
spark.velocity = 125;
spark.emissionRange = 2* M_PI; // 360 deg
spark.yAcceleration = 75; // gravity
spark.lifetime = 3;
spark.contents = (id) [[UIImage imageNamed:@"DazStarOutline"] CGImage];
spark.scaleSpeed =-0.2;
spark.greenSpeed =-0.1;
spark.redSpeed = 0.4;
spark.blueSpeed =-0.1;
spark.alphaSpeed =-0.25;
spark.spin = 2* M_PI;
spark.spinRange = 2* M_PI;
// putting it together
fireworksEmitter.emitterCells = [NSArray arrayWithObject:rocket];
rocket.emitterCells = [NSArray arrayWithObject:burst];
burst.emitterCells = [NSArray arrayWithObject:spark];
[self.layer addSublayer:fireworksEmitter];
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
//绘图代码
CGRect viewBounds=rect;
fireworksEmitter.emitterPosition=CGPointMake(viewBounds.size.width/2.0,viewBounds.size.height);
FireworkSemiter.emitterSize=CGSizeMake(viewBounds.size.width/2.0,0.0);
fireworksEmitter.emitterMode=KCaemitterLayerRoutline;
fireworksEmitter.emitterShape=kCAEmitterLayerLine;
fireworksEmitter.renderMode=KCaemitterLayerEdit;
fireworksimitter.seed=(arc4random()%100)+1;
//创造火箭
CAEmitterCell*火箭=[CAEmitterCell发射单元];
出生率=1.0;
rocket.emissionRange=0.25*M_PI;//角度有一些变化
火箭速度=380;
rocket.velocityRange=100;
火箭加速度=75;
rocket.life=1.02;//我们无法为突发事件设置小于1.0的出生率
rocket.contents=(id)[[UIImage ImageName:@“Dazzing”]CGImage];
火箭。比例=0.2;
rocket.color=[[UIColor redColor]CGColor];
rocket.greenRange=1.0;//不同颜色
rocket.redRange=1.0;
rocket.blueRange=1.0;
rocket.spinRange=M_PI;//慢自旋
//无法看到爆裂对象,但会产生火花
//我们在这里更改颜色,因为火花继承了它的值
CAEmitterCell*突发=[CAEmitterCell-emitterCell];
burst.birthRate=1.0;//在旅行结束时
爆破速度=0;
爆破规模=2.5;
burst.redSpeed=-1.5;//移位
burst.blueSpeed=+1.5;//移位
burst.greenSpeed=+1.0;//移位
爆破寿命=0.35;
//最后,火花
CAEmitterCell*spark=[CAEmitterCell-emitterCell];
出生率=400;
火花速度=125;
spark.emissionRange=2*M_PI;//360度
spark.yaAcceleration=75;//重力
火花寿命=3;
spark.contents=(id)[[UIImage ImageName:@“DazStarOutline”]CGImage];
spark.scaleSpeed=-0.2;
spark.greenSpeed=-0.1;
spark.redSpeed=0.4;
spark.blueSpeed=-0.1;
spark.alphaSpeed=-0.25;
spark.spin=2*M_π;
spark.spinRange=2*M_PI;
//把它放在一起
fireworksimitter.emitterCells=[NSArray arrayWithObject:rocket];
rocket.emitterCells=[NSArray arrayWithObject:burst];
burst.emitterCells=[NSArray arrayWithObject:spark];
[self.layer addSublayer:fireworksimitter];
[自我设置需要显示];
}
您的代码看起来不错,因此这里有一些建议
- 您不会显示您正在创建的
。如果您没有fireworksEmitter
和alloc
将它放在某个地方,您将得到一个没有错误或警告的空白屏幕init
- 如果您没有名为“DazRing”和“DazStarOutline”(拼写检查)的(alpha通道)资源,您将得到一个没有错误或警告的空白屏幕
drawRect
运行,但这不是一个合适的位置。您没有(直接)进行任何绘图,而是在设置和添加图层。您当然不应该从drawRect
内部调用setNeedsDisplay
。如果您将此代码放入(例如)awakeFromNib中(例如)将其从drawRect中删除所有此代码,并将其放入UIView子类init方法中,或者,由于您使用的是storyboard,将其放入awakeFromNib中,则此代码将同样或更好地工作:
- (void)awakeFromNib {
[super awakeFromNib];
CGRect viewBounds = rect;
fireworksEmitter = (CAEmitterLayer*)self.layer;
fireworksEmitter.emitterPosition = CGPointMake(viewBounds.size.width/2.0, viewBounds.size.height);
fireworksEmitter.emitterSize = CGSizeMake(viewBounds.size.width/2.0, 0.0);
fireworksEmitter.emitterMode = kCAEmitterLayerOutline;
fireworksEmitter.emitterShape = kCAEmitterLayerLine;
fireworksEmitter.renderMode = kCAEmitterLayerAdditive;
fireworksEmitter.seed = (arc4random()%100)+1;
// Create the rocket
CAEmitterCell* rocket = [CAEmitterCell emitterCell];
rocket.birthRate = 1.0;
rocket.emissionRange = 0.25 * M_PI; // some variation in angle
rocket.velocity = 380;
rocket.velocityRange = 100;
rocket.yAcceleration = 75;
rocket.lifetime = 1.02; // we cannot set the birthrate < 1.0 for the burst
rocket.contents = (id) [[UIImage imageNamed:@"DazRing"] CGImage];
rocket.scale = 0.2;
rocket.color = [[UIColor redColor] CGColor];
rocket.greenRange = 1.0; // different colors
rocket.redRange = 1.0;
rocket.blueRange = 1.0;
rocket.spinRange = M_PI; // slow spin
CAEmitterCell* burst = [CAEmitterCell emitterCell];
burst.birthRate = 1.0; // at the end of travel
burst.velocity = 0;
burst.scale = 2.5;
burst.redSpeed =-1.5; // shifting
burst.blueSpeed =+1.5; // shifting
burst.greenSpeed =+1.0; // shifting
burst.lifetime = 0.35;
CAEmitterCell* spark = [CAEmitterCell emitterCell];
spark.birthRate = 400;
spark.velocity = 125;
spark.emissionRange = 2* M_PI; // 360 deg
spark.yAcceleration = 75; // gravity
spark.lifetime = 3;
spark.contents = (id) [[UIImage imageNamed:@"DazStarOutline"] CGImage];
spark.scaleSpeed =-0.2;
spark.greenSpeed =-0.1;
spark.redSpeed = 0.4;
spark.blueSpeed =-0.1;
spark.alphaSpeed =-0.25;
spark.spin = 2* M_PI;
spark.spinRange = 2* M_PI;
fireworksEmitter.emitterCells = [NSArray arrayWithObject:rocket];
rocket.emitterCells = [NSArray arrayWithObject:burst];
burst.emitterCells = [NSArray arrayWithObject:spark];
}
-(void)从NIB唤醒{
[超级awakeFromNib];
CGRect viewBounds=rect;
fireworksEmitter=(CAEmitterLayer*)self.layer;
fireworksEmitter.emitterPosition=CGPointMake(viewBounds.size.width/2.0,viewBounds.size.height);
FireworkSemiter.emitterSize=CGSizeMake(viewBounds.size.width/2.0,0.0);
fireworksEmitter.emitterMode=KCaemitterLayerRoutline;
fireworksEmitter.emitterShape=kCAEmitterLayerLine;
fireworksEmitter.renderMode=KCaemitterLayerEdit;
fireworksimitter.seed=(arc4random()%100)+1;
//创造火箭
CAEmitterCell*火箭=[CAEmitterCell发射单元];
出生率=1.0;
rocket.emissionRange=0.25*M_PI;//角度有一些变化
火箭速度=380;
rocket.velocityRange=100;
火箭加速度=75;
rocket.life=1.02;//我们无法为突发事件设置小于1.0的出生率
rocket.contents=(id)[[UIImage ImageName:@“Dazzing”]CGImage];
火箭。比例=0.2;
rocket.color=[[UIColor redColor]CGColor];
rocket.greenRange=1.0;//不同颜色
rocket.redRange=1.0;
rocket.blueRange=1.0;
rocket.spinRange=M_PI;//慢自旋
CAEmitterCell*突发=[CAEmitterCell-emitterCell];
burst.birthRate=1.0;//在旅行结束时
爆破速度=0;
爆破规模=2.5;
burst.redSpeed=-1.5;//移位
burst.blueSpeed=+1.5;//移位
burst.greenSpeed=+1.0;//移位
爆破寿命=0.35;
CAEmitterCell*spark=[CAEmitterCell-emitterCell];
出生率=400;
火花速度=125;
spark.emissionRange=2*M_PI;//360度
亚切拉星火酒店