2012-08-12 9 views
6

Estoy animando la ruta de sombra para CALayer.Animar un camino de sombra CALayer

El marco cambia el tamaño correctamente, pero la sombra no se escala.

lugar a la sombra comienza en el tamaño final CGSize(20,20) y mantiene a lo largo de la animación a pesar de que me puse la Shadowpath a un valor inicial

[CATransaction begin]; 
[CATransaction setAnimationDuration: 0]; 
[CATransaction setDisableActions: TRUE]; 
    layer.frame = CGRectMake(0,0,10,10); 
    layer.shadowPath = [UIBezierPath bezierPathWithRect:layer.bounds].CGPath; 
[CATransaction commit]; 

[CATransaction begin]; 

    [CATransaction setValue:[NSNumber numberWithFloat:10] forKey:kCATransactionAnimationDuration]; 
    layer.frame = CGRectMake(0,0,20,20); 
    layer.shadowPath = [UIBezierPath bezierPathWithRect:tile.bounds].CGPath; 

[CATransaction commit]; 

Respuesta

9

Al principio, pequeño cuadrado con sombra.

ss

Cuando botón pulsado, cuadrado y sombras crecen más grandes juntos.

ss

El código principal es el siguiente:

[CATransaction begin]; 
[CATransaction setAnimationDuration:5.0]; 
CAMediaTimingFunction *timing = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
[CATransaction setAnimationTimingFunction:timing]; 
layer.frame = CGRectMake(0,0,100,100); 
[CATransaction commit];  

CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"]; 
shadowAnimation.duration = 5.0; 
shadowAnimation.fromValue = (id)[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 50, 50)].CGPath; 
shadowAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)].CGPath; 
[layer addAnimation:shadowAnimation forKey:@"shadow"]; 

Puede descargar este proyecto de GitHub y sólo ejecutarlo.

https://github.com/weed/p120812_CALayerShadowTest

Esta pregunta fue muy difícil para mí! :)

+0

gracias por el esfuerzo. ¿Puedes decirme por qué mi solución no funciona? ¿Por qué puedes animar el marco pero no el camino de sombra? – prostock

+0

El punto de la animación de la sombra es establecer 'animationWithKeyPath' como' @ "shadowPath" '. Esto significa que el objeto de animación es 'shadowPath'. Tampoco conocía este mecanismo, pero la configuración 'animationWithKeyPath' es muy importante para hacer la animación. Esta bien ? – weed

3

Queríamos añadir otra respuesta basada en la respuesta de Weed. Tomé la respuesta de Weed e intenté poner todo en una CATransacción porque quería animar múltiples capas y asegurarme de que las animaciones sucedieran juntas. Aquí van ustedes si alguna vez lo necesitan. Además, todavía no entiendo por qué tienes que usar the fromValue y toValue en una CATransaction. ¿Por qué no puedes hacer lo mismo que haces con otras propiedades como marco?

[CATransaction begin]; 

[CATransaction setValue:[CAMediaTimingFunction 
    functionWithName:kCAMediaTimingFunctionEaseOut] 
    forKey:kCATransactionAnimationTimingFunction]; 

for (CALayer *layer in self.layers){ 
    CABasicAnimation *shadowAnimation = 
    [CABasicAnimation animationWithKeyPath:@"shadowPath"]; 

    shadowAnimation.fromValue = 
     (id)[UIBezierPath bezierPathWithRect:layer.bounds].CGPath; 
    shadowAnimation.timingFunction = 
     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    layer.frame = rectFinal; 
    shadowAnimation.toValue = 
     (id)[UIBezierPath bezierPathWithRect:layer.bounds].CGPath; 
    layer.shadowPath = 
     [UIBezierPath bezierPathWithRect:layer.bounds].CGPath; 
    [layer addAnimation:shadowAnimation forKey:nil]; 
}   
[CATransaction commit];