iOS 7 CAEmitterLayer不适当地产生粒子

奇怪的问题我似乎无法解决只在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已经不再是testing版,我想我应该在这里问一下,看看有没有人可以破解它。 我真的希望这不只是一个错误,我们必须等待7.0.17.1得到修复。 任何想法将不胜感激。 谢谢!

是!

我自己花了几个小时在这个问题上。

在我们使用一些策略之前,要获得同样的birthRateanimation。

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

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

就好像它计算了图层将来的状态。

你真是太亲密了,但实际上,排放者是从过去开始的。

其次,要在0和n的出生率之间进行animation处理,而我们之前可以操作生命期属性的效果是:

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

请注意,我设置发射层本身的lifetime 。 这是因为当发射发射单元版本的这个属性乘以发射层的值。 设置发射器层的lifetime设置了所有发射器单元的lifetime的倍数,使您可以轻松地打开和closures它们。

对于我来说,我的CAEmitterLayer,移动到iOS7的问题如下:

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

我唯一需要改变的是删除cell.duration = XXX,然后我的粒子又开始出现了。 我要吃一个苹果在这个意外的,无法解释的麻烦。