2013-03-27 15 views
17

Come animare il percorso CAShapeLayer e fillColor?Come animare il percorso CAShapeLayer e fillColor

Come animare strokeEnd per mostrare il percorso che si sta disegnando? e come animare fillColor?

+0

Anima automaticamente quando si imposta. Puoi dare qualche informazione in più su cosa stai cercando di ottenere? – jrturton

risposta

49

Per animare percorso

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

Spero che questo aiuta :)

+0

Vorrei aggiungere che fromValue e toValue potrebbero essere oggetti CGMutablePathRef per animare la modifica del percorso stesso, utilizzando la chiave @ "percorso". Quanto sopra con quel cambiamento ha funzionato per i miei scopi. Grazie. – webjprgm

Problemi correlati