2012-06-03 13 views
22

me gustaría animar un UIBezierPath de rect a uno triangular, no para animar una vista en un camino sino para animar el camino en sí mismo para que se modifique de una forma a otra. la ruta se añade a una CALayerCómo animar un UIBezierPath

CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.fillColor = [[UIColor whiteColor] CGColor];  
maskLayer.path = [self getRectPath]; 

-(CGPathRef)getTrianglePath 
{ 
    UIBezierPath* triangle = [UIBezierPath bezierPath]; 
    [triangle moveToPoint:CGPointZero]; 
    [triangle addLineToPoint:CGPointMake(width(self.view),0)]; 
    [triangle addLineToPoint:CGPointMake(0, height(self.view))]; 
    [triangle closePath]; 
    return [triangle CGPath]; 
} 

-(CGPathRef)getRectPath 
{ 
    UIBezierPath*rect = [UIBezierPath bezierPathWithRect:self.view.frame]; 
    return [rect CGPath]; 
} 

Respuesta

28

yo no soy un experto en CoreAnimation pero se puede definir un CABasicAnimation de la siguiente manera

CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"]; 
morph.duration = 5; 
morph.toValue = (id) [self getTrianglePath]; 
[maskLayer addAnimation:morph forKey:nil]; 

La primera línea indica que se quiere definir una animación que cambia la path propiedad de la capa. La segunda línea indica que la animación dura cinco segundos y la tercera línea da el estado final de la animación. Finalmente, agregamos la animación a la capa. La clave es un identificador único para la animación, es decir, si agrega dos animaciones con la misma clave, solo se considera la última. Esta clave también se puede usar para anular las llamadas animaciones implícitas. Hay un par de propiedades de CALayer que están animadas de manera predeterminada. Más precisamente, si cambia una de estas propiedades, el cambio se animará con una duración de 0,25.

+0

gracias, simple. estaba usando CAKeyframeAnimation. –