2013-03-27 57 views
17

如何动画CAShapeLayer路径和fillColor?如何动画CAShapeLayer路径和fillColor

如何动画strokeEnd以显示正在绘制的路径?以及如何动画fillColor?

+0

当你设置它自动动画。你能否提供更多关于你想要达到的内容的信息? – jrturton 2013-03-27 07:53:05

回答

49

要动态路径

//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"]; 

我希望这有助于:)

+0

我会添加fromValue和toValue可能是CGMutablePathRef对象来动画改变路径本身,使用键@“路径”。上述改变对我的目的起作用。谢谢。 – webjprgm 2014-10-06 22:35:13