2013-03-27 17 views
17

Jak animować ścieżkę do pliku CAShapeLayer i wypełnić kolor?Jak animować ścieżkę i wypełnienie CAShapeLayer

Jak animować strokeEnd, aby pokazać narysowaną ścieżkę? i jak animować fillColor?

+0

ona animuje automatycznie, kiedy go ustawić. Czy możesz podać nieco więcej informacji o tym, co próbujesz osiągnąć? – jrturton

Odpowiedz

49

Aby animować ścieżka

//setup the CAShapeLayer 
myShapeLayer.strokeColor = [[UIColor blueColor] CGColor]; 
myShapeLayer.lineWidth = 5; 
myShapeLayer.fillColor = [[UIColor yellowColor] CGColor]; 



//Display it! 
[self.view.layer addSublayer:myShapeLayer]; 

//Animate path 
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
pathAnimation.duration = 1.5f; 
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 
pathAnimation.repeatCount = 10; 
pathAnimation.autoreverses = YES; 
[myShapeLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; 

//Animate colorFill 
CABasicAnimation *fillColorAnimation = [CABasicAnimation animationWithKeyPath:@"fillColor"]; 
fillColorAnimation.duration = 1.5f; 
fillColorAnimation.fromValue = (id)[[UIColor clearColor] CGColor]; 
fillColorAnimation.toValue = (id)[[UIColor yellowColor] CGColor]; 
fillColorAnimation.repeatCount = 10; 
fillColorAnimation.autoreverses = YES; 
[myShapeLayer addAnimation:fillColorAnimation forKey:@"fillColor"]; 

Mam nadzieję, że to pomoże :)

+0

Dodam, że fromValue i toValue mogą być obiektami CGMutablePathRef, aby animować zmianę samej ścieżki, używając klucza @ "ścieżka". Powyższe z tą zmianą działało dla moich celów. Dzięki. – webjprgm