2017-12-15 5 views
2

アニメーションが開始されたときにstrokeEndキーパスを確認したい。しかし、それはうまくいかない、どこがうまくいかなかったのだろうか?レイヤー 'strokeEnd'アニメーションイベントを観察する

- (void)addAnimation { 
    // do animation 
    CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
    drawAnimation.duration   = 3.f; 
    drawAnimation.repeatCount   = 1.0; 
    drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
    drawAnimation.toValue = [NSNumber numberWithFloat:0.5f]; 
    drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    drawAnimation.fillMode = kCAFillModeForwards; 
    drawAnimation.removedOnCompletion = NO; 
    [self.progressLayer addAnimation:drawAnimation forKey:@"drawCircleAnimation"]; 
    [self.progressLayer addObserver:self forKeyPath:@"strokeEnd" options:NSKeyValueObservingOptionNew context:NULL]; // 监听position 

} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    NSLog(@"change:%@",change); 
    // not called here... 
} 
+0

違いがあるかどうかは分かりませんが、NULLの代わりに 'nil'としてコンテキストを指定してみてください。他の問題は表示されません。 – Skywalker

+0

あなたは確かですかstrokeEndはKVOです準拠? – e1985

答えて

2

アニメーションが「飛行中」である間に、アニメーション化されたプロパティーが変化するのを観察することはできません。このプロパティは実際にはアニメーションの開始時に終了値に設定されます。次に、ビューの通常レイヤーの上に配置された「プレゼンテーションレイヤー」があり、そのレイヤー上でアニメーションが実行されます。

CADisplayLinkタイマー(画面のリフレッシュレートに同期する軽量タイマー)を設定し、プレゼンテーションレイヤーのプロパティをアニメーション化するときに照会することです(layer.presentationLayer.strokeEnd、あなたのケースでは)

+0

私はあなたの歩みに従っており、すべてが期待通りに機能します!ありがとうございました – tounaobun