nek*_*Tip 8 ios caemitterlayer caemittercell
我想创建一个只在用户触摸屏幕时发出的粒子效果,但是一旦设置为非零值,我就无法更改CAEmitterCell birthRate属性.
我有一个UIView的子类,它按照我想要的方式设置我的CAEmitterLayer和我的CAEmitterCell.我在该类上定义了两个属性:
@property (strong, nonatomic) CAEmitterLayer *emitterLayer;
@property (strong, nonatomic) CAEmitterCell *emitterCell;
Run Code Online (Sandbox Code Playgroud)
然后,在我的视图控制器中,我正在跟踪触摸,设置emitterLayer的位置和emitterCell的出生率:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint tappedPt = [touch locationInView:touch.view];
NSLog(@"began x:%f y:%f",tappedPt.x, tappedPt.y);
emitterView.emitterCell.birthRate = 42;
emitterView.emitterLayer.emitterPosition = tappedPt;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint tappedPt = [touch locationInView:touch.view];
NSLog(@"moved x:%f y:%f",tappedPt.x, tappedPt.y);
emitterView.emitterLayer.emitterPosition = tappedPt;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"ending %f", emitterView.emitterCell.birthRate);
emitterView.emitterCell.birthRate = 0.00;
NSLog(@"ended %f", emitterView.emitterCell.birthRate);
}
Run Code Online (Sandbox Code Playgroud)
日志报告emitterView.emitterCell.birthRate更改:
began x:402.000000 y:398.500000
ending 42.000000
ended 0.000000
Run Code Online (Sandbox Code Playgroud)
当我触摸屏幕时,发射器按预期启动,图层跟随触摸,但当我结束触摸时,发射器单元愉快地发出最初设置的任何值(touchesBegan中设置的值).无论我做什么,一旦设置为非零值,我似乎无法改变出生率值.日志报告值已正确设置,但发射器不断发光.
但是,如果我更改touchesEnded方法来更改图层的位置,在我在emitterCell上设置birthRate后,一切都按预期工作:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint tappedPt = [touch locationInView:touch.view];
NSLog(@"began x:%f y:%f",tappedPt.x, tappedPt.y);
NSLog(@"ending %f", emitterView.emitterCell.birthRate);
emitterView.emitterCell.birthRate = 0.0;
NSLog(@"ended %f", emitterView.emitterCell.birthRate);
emitterView.emitterLayer.emitterPosition = tappedPt;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下原因吗?
要停止发射粒子,您必须将实例的birthRate属性设置CAEmitterLayer为 0,尽管它最初是在CAEmitterCell实例上设置的......不知道为什么,但它有效。
斯威夫特 3 示例:
func emitParticles() {
let particlesEmitter = CAEmitterLayer()
particlesEmitter.emitterPosition = center
particlesEmitter.emitterShape = kCAEmitterLayerCircle
particlesEmitter.emitterSize = CGSize(width: 50, height: 50)
particlesEmitter.renderMode = kCAEmitterLayerAdditive
let cell = CAEmitterCell()
cell.birthRate = 15
cell.lifetime = 1.0
cell.color = bubble.color.cgColor
cell.velocity = 150
cell.velocityRange = 50
cell.emissionRange = .pi
cell.scale = 0.1
cell.scaleSpeed = -0.1
cell.contents = UIImage(named: "particle")?.cgImage
particlesEmitter.emitterCells = [cell]
layer.addSublayer(particlesEmitter)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
particlesEmitter.birthRate = 0
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它表现得有些奇怪但我发现使用键值编码解决了问题并且单元停止发射:
假设您的CAEmitterLayer是"yourEmitterLayer"并且您有一个名为"yourEmitterCell"的CAEmitterCell,如果置于touchesEnded内,这将停止发射:
[yourEmitterLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"emitterCells.yourEmitterCell.birthRate"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1658 次 |
| 最近记录: |