Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 CAEmitterLayer spawning particles inappropriately

Strange issue I can't seem to resolve where on iOS 7 only, CAEmitterLayer will spawn particles on the screen incorrectly when birth rate is initially set to a nonzero value. It's as if it calculates the state the layer would be in the future.

// 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];

I saw on the DevForums one post where a few people mentioned they had similar problems with iOS 7 and CAEmitterLayer, but no one had any ideas how to fix it. Now that iOS 7 is no longer beta, I figured I should ask here and see if anyone can crack it. I really hope this isn't just a bug that we have to wait for 7.0.1 or 7.1 to get fixed. Any ideas would be much appreciated. Thanks!

like image 437
Ryan F Avatar asked Sep 20 '13 09:09

Ryan F


2 Answers

YES!

I spent hours on this problem myself.

To get the same kind of animation of the birthRate we had before we use a couple of strategies.

Firstly, if you want the layer to look like it begins emitting when added to the view you need to remember that CAEmitterLayer is a subclass of CALayer which conforms to the CAMediaTiming protocol. We have to set the whole emitter layer to begin at the current moment:

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

It's as if it calculates the state the layer would be in the future.

You were eerily close, but actually its that the emitter was beginning in the past.

Secondly, to animate between a birthrate of 0 and n, with the effect that we had before we can manipulate the lifetime property instead:

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

Note that i set the lifetime on the emitter layer itself. This is because when emitting the emitter cell's version of this property gets multiplied by the value in the emitter layer. Setting the lifetime of the emitter layer sets a multiple of the lifetimes of all your emitter cells, allowing you to turn them all on and off with ease.

like image 155
jackslash Avatar answered Oct 24 '22 06:10

jackslash


For me, the issue with my CAEmitterLayer, when moving to iOS7 was the following:

In iOS7 setting the CAEmitterLayerCell's duration resulted in the particle not showing at all!

The only thing I had to change was remove the cell.duration = XXX and then my particles began showing up again. I am going to eat an Apple over this unexpected, unexplained hassle.

like image 29
Danny Avatar answered Oct 24 '22 04:10

Danny