iOS 7 CAEmitterLayer不恰当地产生粒子 [英] iOS 7 CAEmitterLayer spawning particles inappropriately

查看:137
本文介绍了iOS 7 CAEmitterLayer不恰当地产生粒子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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!

推荐答案

是的!

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

I spent hours on this problem myself.

获得<$ c的相同类型的动画$ c> birthRate 我们之前使用过几种策略。

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

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

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.

其次,在0和n的出生率之间进行动画制作,以及我们之前可以操作life life属性的效果:

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.

这篇关于iOS 7 CAEmitterLayer不恰当地产生粒子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆