2017-12-15 6 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 is KVO 준수? – e1985

답변

2

애니메이션이 "비행 중"인 동안 애니메이션 속성이 변경되는 것을 볼 수 없습니다. 이 속성은 실제로 애니메이션의 시작 부분에서 끝 값으로 설정됩니다. 그런 다음보기의 일반 레이어 위에 배치 된 "프리젠 테이션 레이어"가 있으며 해당 레이어에서 애니메이션이 발생합니다.

가장 좋은 방법은 CADisplayLink 타이머 (화면 새로 고침 빈도와 동기화 된 경량 타이머)를 설정하고 애니메이션이 적용될 때 프리젠 테이션 레이어의 속성을 쿼리하는 것입니다 (layer.presentationLayer.strokeEnd, 귀하의 경우.)

+0

나는 당신의 계단을 따라 왔으며 모든 것이 예상대로 작동합니다! 고맙습니다 – tounaobun

관련 문제