【问题标题】:iOS 7 CAEmitterLayer spawning particles inappropriatelyiOS 7 CAEmitterLayer 不恰当地生成粒子
【发布时间】:2013-09-20 09:39:57
【问题描述】:

奇怪的问题我似乎无法解决仅在 iOS 7 的哪个位置,当出生率最初设置为非零值时,CAEmitterLayer 会在屏幕上错误地生成粒子。就好像它计算了该层未来的状态。

// Create black image particle
CGRect rect = CGRectMake(0, 0, 20, 20);
UIGraphicsBeginImageContext(rect.size);
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Create cell
CAEmitterCell *cell = [CAEmitterCell emitterCell];
cell.contents = (__bridge id)img.CGImage;
cell.birthRate = 100.0;
cell.lifetime = 10.0;
cell.velocity = 100.0;

// Create emitter with particles emitting from a line on the
// bottom of the screen
CAEmitterLayer *emitter = [CAEmitterLayer layer];
emitter.emitterShape = kCAEmitterLayerLine;
emitter.emitterSize = CGSizeMake(self.view.bounds.size.width,0);
emitter.emitterPosition = CGPointMake(self.view.bounds.size.width/2,
                                      self.view.bounds.size.height);
emitter.emitterCells = @[cell];

[self.view.layer addSublayer:emitter];

我在 DevForums 上看到一篇帖子,其中一些人提到他们在 iOS 7CAEmitterLayer 上遇到了类似的问题,但没有人知道如何解决它。现在iOS 7 不再是测试版,我想我应该在这里问一下,看看是否有人可以破解它。我真的希望这不仅仅是一个我们必须等待7.0.17.1 修复的错误。任何想法将不胜感激。谢谢!

【问题讨论】:

    标签: ios objective-c core-animation ios7 caemitterlayer


    【解决方案1】:

    是的!

    我自己花了几个小时来解决这个问题。

    为了获得与我们使用几种策略之前的birthRate 相同类型的动画。

    首先,如果您希望该层在添加到视图时看起来像开始发射,您需要记住CAEmitterLayerCALayer 的子类,它符合CAMediaTiming 协议。我们必须将整个发射器层设置为从当前时刻开始:

    emitter.beginTime = CACurrentMediaTime();
    [self.view.layer addSublayer:emitter];
    

    就好像它计算了层在未来的状态。

    你离奇地接近,但实际上发射器是在过去开始的。

    其次,在出生率 0 和 n 之间进行动画处理,效果与我们之前可以操作生命周期属性一样:

    if (shouldBeEmitting){
        emitter.lifetime = 1.0;
    }
    else{
        emitter.lifetime = 0;
    }
    

    请注意,我在发射器层本身上设置了lifetime。这是因为在发射发射器单元时,此属性的版本会乘以发射器层中的值。设置发射器层的lifetime 会设置所有发射器单元的lifetimes 的倍数,让您可以轻松地打开和关闭它们。

    【讨论】:

    • 哇哦,非常感谢!我刚刚放弃并编写了自己的粒子生成器,它的性能不太好。设置 beginTime 是我需要做的所有事情,因为我没有为出生率设置动画。我正在开发的应用程序将在不到两天的时间内提交,所以你节省了一天!谢谢!
    • 此解决方案修复了大混乱,但仍无法与旧行为 100% 相同。我发射的粒子更少,而且还有一些其他奇怪的副作用。
    • 非常感谢!这已经困扰我好几个月了。
    【解决方案2】:

    对我来说,我的 CAEmitterLayer 在迁移到 iOS7 时的问题如下:

    在 iOS7 中设置 CAEmitterLayerCell 的持续时间导致粒子根本不显示!

    我唯一需要更改的是删除 cell.duration = XXX,然后我的粒子又开始出现了。我要为这个意想不到的、无法解释的麻烦吃掉一个苹果。

    【讨论】:

    • 宾果游戏!谢谢!由于您的回答,我只注释掉了 [_CAEmitterCell setDuration:2.0];在我的代码中输入一行,在 iOS 7 模拟器下重新运行,它就可以工作了!
    猜你喜欢
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多