ios – CAEmitterCell不尊重birthRate的变化

前端之家收集整理的这篇文章主要介绍了ios – CAEmitterCell不尊重birthRate的变化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个只在用户触摸屏幕时发出的粒子效果,但是一旦设置为非零值,我就无法更改CAEmitterCell birthRate属性.

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

@property (strong,nonatomic) CAEmitterLayer *emitterLayer;
@property (strong,nonatomic) CAEmitterCell *emitterCell;

然后,在我的视图控制器中,我正在跟踪触摸,设置emitterLayer的位置和emitterCell birthrate:

-(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.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发生了变化:

began x:402.000000 y:398.500000
ending 42.000000
ended 0.000000

当我触摸屏幕时,发射器按预期启动,图层跟随触摸,但当我结束触摸时,发射器单元愉快地发出最初设置的任何值(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.y);

    NSLog(@"ending %f",emitterView.emitterCell.birthRate);
    emitterView.emitterCell.birthRate = 0.0;
    NSLog(@"ended %f",emitterView.emitterCell.birthRate);
    emitterView.emitterLayer.emitterPosition = tappedPt;
}

有人可以解释一下原因吗?

解决方法

我不知道为什么它表现得有点奇怪但我发现使用键值编码解决了问题并且单元格停止发出:

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

[yourEmitterLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"emitterCells.yourEmitterCell.birthRate"];
原文链接:https://www.f2er.com/iOS/333475.html

猜你在找的iOS相关文章