2009-07-09 5 views
4

CALLayersを使用して気泡を下から上に流すコードがあります。ユーザーが画面にタッチすると、現在実行中のアニメーションを、指が触れたtoPointを持つアニメーションに置き換えるコードがあります。アニメーションが切り替わると、デバイス上で(シミュレータ上ではなく)ちらつきが発生します。ちらつきを解消するためのヒントをいただければ幸いです!ありがとう。層自体の内部まで流れる気泡ためpositionプロパティmidのCABasicAnimationをアニメーションの途中で切り替えると、フリッカーが発生します

コード:

CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"position"]; 
[animation setDelegate:self]; 
CGPoint position = [self position]; 
NSValue *prevVal = [NSValue valueWithCGPoint:position]; 
[animation setFromValue:prevVal]; 
CGPoint toPoint = CGPointMake(position.x,-100); 
[animation setToValue:[NSValue valueWithCGPoint:toPoint]]; 
[animation setDuration:animationDuration]; 
[self addAnimation:animation forKey:@"flow"]; 

スーパー層に書き込まれたタッチポイントに近くの気泡を吸引するためのコード:

int count = [self.layer.sublayers count]; 
for(int i = 0; i < count ; i++) { 
    CALayer *layer= [self.layer.sublayers objectAtIndex:i]; 
    CALayer *p = (CALayer*)[layer presentationLayer]; 
    CGPoint position = [p position]; 

    if(abs(position.x - touchPoint.x) < 100 && abs(position.y - touchPoint.y) < 100) { 

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
    [animation setDelegate:self]; 
    NSValue *prevVal = [NSValue valueWithCGPoint:position]; 
    [animation setFromValue:prevVal]; 
    [animation setToValue:[NSValue valueWithCGPoint:touchPoint]]; 
    [animation setDuration:2.0]; 
    [animation setTimingFunction:[CAMediaTimingFunction 
      functionWithName:kCAMediaTimingFunctionEaseOut]]; 
    [layer addAnimation:animation forKey:@"flow"]; 
    }   

}

+0

あなたはどのように多くのサブレイヤを持っていますか?覚えておいて、シミュレータはほとんど常にデバイスより高速です。また、より穏やかなアニメーション遷移のためにEaseInEaseOutを試してみてください。 – amattn

+0

この回答を確認することができます:http://stackoverflow.com/a/25611323/1953178 –

答えて

5

アップデートの周りにCATransactionロックを使用して、役立つかどうか確認してください。これにより、以前のアニメーションでは、新しいアニメーションでそれらを更新する過程で、レイヤの位置が変更されなくなります。あなたのタッチ処理方法で

、トランザクションとロックでアニメーションをラップ:

[CATransaction lock]; 
[CATransaction begin]; 

// update the sublayers with new animations 

[CATransaction commit]; 
[CATransaction unlock]; 
関連する問題