首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAEmitterCell不支持birthRate更改

CAEmitterCell不支持birthRate更改
EN

Stack Overflow用户
提问于 2013-05-25 20:12:18
回答 2查看 2K关注 0票数 9

我想创建一个粒子效果,它只在用户触摸屏幕时发射,但是一旦设置为非零值,我就不能更改CAEmitterCell birthRate属性。

我有一个UIView的子类,它可以按照我想要的方式设置我的CAEmitterLayer和CAEmitterCell。我在该类上定义了两个属性:

代码语言:javascript
复制
@property (strong, nonatomic) CAEmitterLayer *emitterLayer;
@property (strong, nonatomic) CAEmitterCell *emitterCell;

然后,在我的视图控制器中,我跟踪触摸,设置emitterLayer的位置和emitterCell出生速率:

代码语言:javascript
复制
-(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);
}

日志报告emitterView.emitterCell.birthRate发生了变化:

代码语言:javascript
复制
began x:402.000000 y:398.500000
ending 42.000000
ended 0.000000

当我触摸屏幕时,发射器按预期启动,层跟随触摸,但当我结束触摸时,发射器单元愉快地发射初始设置的任何值(在touchesBegan中设置的值)。无论我做什么,一旦将出生率值设置为非零值,似乎就无法更改出生率值。日志报告这些值设置正确,但发射器仍在发射。

但是,如果我更改touchesEnded方法以更改层的位置,则在emitterCell上设置birthRate之后,一切都会按预期运行:

代码语言:javascript
复制
-(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;
}

有人能解释一下为什么吗?

EN

回答 2

Stack Overflow用户

发布于 2017-05-15 04:33:20

要停止发射粒子,您必须将CAEmitterLayer实例的birthRate属性设置为0,尽管它最初是在CAEmitterCell实例上设置的...不知道为什么,但它很有效。

Swift 3示例:

代码语言:javascript
复制
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
    }
}
票数 7
EN

Stack Overflow用户

发布于 2013-05-28 04:28:01

我不知道为什么它的行为有点奇怪,但我发现使用键值编码解决了这个问题,细胞停止发射:

假设您的CAEmitterLayer是"yourEmitterLayer“,并且您有一个名为"yourEmitterCell”的CAEmitterCell,如果放在touchesEnded中,这将停止发射:

代码语言:javascript
复制
[yourEmitterLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"emitterCells.yourEmitterCell.birthRate"];
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16749430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档