2013-03-27 121 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