La propiedad de marco de un CALayer es una propiedad derivada, que depende de la posición, punto de anclaje, límites y transformación de la capa. En lugar de animar el marco, en su lugar debe animar la posición o los límites, según el efecto que intente lograr.
Para mover una capa, puede animar la position
:
-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
{
// Prepare the animation from the current position to the new position
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [layer valueForKey:@"position"];
// NSValue/+valueWithPoint:(NSPoint)point is available on Mac OS X
// NSValue/+valueWithCGPoint:(CGPoint)point is available on iOS
// comment/uncomment the corresponding lines depending on which platform you're targeting
// Mac OS X
animation.toValue = [NSValue valueWithPoint:NSPointFromCGPoint(point)];
// iOS
//animation.toValue = [NSValue valueWithCGPoint:point];
// Update the layer's position so that the layer doesn't snap back when the animation completes.
layer.position = point;
// Add the animation, overriding the implicit animation.
[layer addAnimation:animation forKey:@"position"];
}
Para cambiar el tamaño de una capa, que sería animar la bounds
parámetro:
-(void)resizeLayer:(CALayer*)layer to:(CGSize)size
{
// Prepare the animation from the old size to the new size
CGRect oldBounds = layer.bounds;
CGRect newBounds = oldBounds;
newBounds.size = size;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];
// NSValue/+valueWithRect:(NSRect)rect is available on Mac OS X
// NSValue/+valueWithCGRect:(CGRect)rect is available on iOS
// comment/uncomment the corresponding lines depending on which platform you're targeting
// Mac OS X
animation.fromValue = [NSValue valueWithRect:NSRectFromCGRect(oldBounds)];
animation.toValue = [NSValue valueWithRect:NSRectFromCGRect(newBounds)];
// iOS
//animation.fromValue = [NSValue valueWithCGRect:oldBounds];
//animation.toValue = [NSValue valueWithCGRect:newBounds];
// Update the layer's bounds so the layer doesn't snap back when the animation completes.
layer.bounds = newBounds;
// Add the animation, overriding the implicit animation.
[layer addAnimation:animation forKey:@"bounds"];
}
se pueden combinar estas animaciones usando un CAAnimationGroup si necesita mover y cambiar el tamaño de una capa al mismo tiempo.
Gracias Vladimir. He descubierto que CALayer no puede animar la propiedad del marco directamente. La documentación dice eso en un cuadro gris. No lo he visto por alguna razón. – dontWatchMyProfile
La última línea está bien tal como está: la clave que especifique en addAnimation puede ser una NSString arbitraria. Creo que la respuesta real es la de berilio por debajo de – Mattia
'[myLayer addAnimation: frameAnimation forKey: @" frame "];' 'frame' es solo un nombre para la animación y no tiene nada que ver con la propiedad real que se está animando ... –