2010-04-01 11 views

Respuesta

15

Hay algunas formas diferentes de resolver esto. Mi favorito es establecer el objeto al final de la animación y usar fromValue en lugar de toValue para crear la animación. También puede establecer tanto desde Valor como a Valor para crear este efecto. es decir

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
animation.duration = 1.0; 
animation.fromValue = [NSValue valueWithCGPoint:startPoint]; 
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 

viewToAnimate.center = endPoint; 
[viewToAnimate.layer addAnimation:animation forKey:@"slide"]; 

La otra manera de hacer esto es establecer a sí mismo como el delegado y poner en práctica:

+0

Genial. funciona para mí, muchas gracias –

+1

aquí, ¿cuál es el valor de "easing" ?. – sathiamoorthy

+0

Creo que si no se establece la aceleración, se carga previamente en Linear, pero debería ser sencillo agregarla si está buscando facilidad de entrada/salida o entrada/salida. –

8

Es necesario ajustar la posición final. Si solo usa la animación deValor y aValor, volverá al punto de inicio original. La animación usa una copia de la vista/capa mientras se "reproduce" y luego la animación mostrará la vista/capa original cuando termine.

Si no lo establece explícitamente, parecerá que retrocede, aunque la vista/capa nunca se movió realmente.

startPoint y endPoint son CGPoints y myView es la UIView que se está animando.

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
    animation.duration = .3; 
    animation.fromValue = [NSValue valueWithCGPoint:startPoint]; 
    animation.toValue = [NSValue valueWithCGPoint:endPoint]; 
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    [myView.layer addAnimation:animation forKey:@"position"]; 
    myView.layer.position = endPoint; // Must set end position, otherwise it jumps back 
+1

Además, no olvides establecer la propiedad ** removedOnCompletion ** de la animación en ** NO ** 'animation.removedOnCompletion = NO;' – krpec

0

Si nos fijamos en la documentación para addAnimation: forKey :, que dice 'Normalmente, esto se invoca de forma implícita'. En otras palabras, se supone que no debes llamarlo directamente. En realidad estás destinado a hacer lo siguiente en su lugar, lo que es aún más fácil de establecer desde y hacia los valores, sólo tiene que establecer los valores directamente sobre la capa:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
animation.duration = .3; 

myLayer.actions = @{@"opacity": animation}; 
myLayer.opacity = 0; 

Esto se desvanecerá una capa a cero opacidad.

+0

No veo eso en los documentos, podría ser algo de OSX ¿solamente? Estoy mirando CALayer [aquí] (https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instm/CALayer/addAnimation: forKey :). La mayoría de los tutoriales de Apple incluyen una llamada a 'addAnimation: forKey:' p. [aquí] (https://developer.apple.com/library/ios/documentation/cocoa/conceptual/coreanimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html). – bcattle

3

Para fijar el objeto al final de la animación acaba de escribir el código como este

#import <QuartzCore/QuartzCore.h> 

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
animation.duration = 1.0; 
animation.fromValue = [NSValue valueWithCGPoint:startPoint]; 
animation.timingFunction = [CAMediaTimingFunction functionWithName:easing]; 
animation.fillMode = kCAFillModeForwards; // This line will stop the animation at the end position of animation 
animation.autoreverses = NO; 

viewToAnimate.center = endPoint; 
[viewToAnimate.layer addAnimation:animation forKey:@"slide"]; 
+1

no funciona para mí –

1

Puede acaba de establecer removedOnCompletion a NO si desea conservar el valor final. Y no te olvides de configurar el modo de relleno.

animation.fillMode = kCAFillModeForwards; //The receiver remains visible in its final state when the animation is completed. 
animation.removedOnCompletion = NO; 
Cuestiones relacionadas