iOS 7 CAEmitterLayer spawning particles inappropriately

廉价感情. 提交于 2019-11-28 16:34:18

问题


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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18913484/ios-7-caemitterlayer-spawning-particles-inappropriately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!