首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未显示CAEmitterLayer

未显示CAEmitterLayer
EN

Stack Overflow用户
提问于 2013-04-19 00:01:37
回答 2查看 1.6K关注 0票数 2

我只需在storyboard view controller中添加一个UIView,然后创建一个UIView类来显示粒子效果。我将UIView's类名更改为我创建的自定义类的名称。我在UIView类中使用的代码没有显示我期望的粒子效果。代码如下:

代码语言:javascript
复制
- (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];
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-19 00:56:15

1)从drawRect中删除所有这些代码,并将其放入UIView子类的初始化方法中,或者,因为您使用的是故事板,所以将其放入awakeFromNib中:

代码语言:javascript
复制
- (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];
}
票数 1
EN

Stack Overflow用户

发布于 2013-04-19 00:55:17

您的代码看起来很好,所以这里有一些建议

  • 你没有显示你创建fireworksEmitter的位置。
  • 如果您没有名为"DazRing“和"DazStarOutline”(检查拼写)的( alloc - init )资源,您将获得一个没有错误或警告的空白屏幕

尽管这段代码将从drawRect内部运行,但这不是一个合适的位置。您并没有(直接)进行任何绘图,而是设置并添加了一个图层。当然,您也不应该从drawRect内部调用setNeedsDisplay。例如,如果您将其放入awakeFromNib中,则此代码将同样有效或更好地工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16087793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档