9

ユーザーが画面に触れている間だけ放出するパーティクル エフェクトを作成したいのですが、CAEmitterCell のbirthRate プロパティをゼロ以外の値に設定すると変更できません。

CAEmitterLayer と CAEmitterCell を希望どおりにセットアップする UIView のサブクラスがあります。そのクラスに 2 つのプロパティを定義しています。

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

次に、View Controller で、タッチを追跡し、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);
}

ログは、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.x, tappedPt.y);

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

誰かが理由を説明してもらえますか?

4

2 に答える 2

6

少し奇妙な動作をしている理由はわかりませんが、キー値コーディングを使用すると問題が解決し、セルの出力が停止することがわかりました。

あなたの CAEmitterLayer が「yourEmitterLayer」であり、「yourEmitterCell」という名前の CAEmitterCell があると仮定すると、これは touchesEnded 内に配置されると放出を停止します。

[yourEmitterLayer setValue:[NSNumber numberWithInt:0] forKeyPath:@"emitterCells.yourEmitterCell.birthRate"];
于 2013-05-27T20:28:01.223 に答える