2010-04-01 11 views

risposta

15

Ci sono diversi modi per risolvere questo problema. Il mio preferito è impostare l'oggetto alla fine dell'animazione e utilizzare fromValue anziché toValue per creare l'animazione. Puoi anche impostare sia fromValue che toValue per creare questo effetto. cioè

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

L'altro modo per farlo è quello di impostare se stessi come il delegato e implementare:

+0

Grande. funziona per me, grazie mille –

+1

qui, qual è il valore di "easing" ?. – sathiamoorthy

+0

Credo che se l'easing non è impostato precarichi su Linear, ma dovrebbe essere semplice aggiungerlo se stai cercando facilità in/out o facilità dentro/fuori. –

8

È necessario impostare la posizione finale. Se si utilizza solo l'animazione fromValue e toValue, si tornerà al punto iniziale originale. L'animazione usa una copia della vista/livello mentre "suona" e quindi l'animazione mostrerà la vista/il livello originale al termine.

Se non lo si imposta in modo esplicito, apparirà uno snap indietro, anche se la vista/livello non si è mai realmente spostata.

startPoint e endPoint sono CGPoint e myView è l'UIView animato.

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

Inoltre, non dimenticare di impostare ** removeOnCompletion ** property of animation su ** NO ** 'animation.removedOnCompletion = NO;' – krpec

0

Se guardate la documentazione per addAnimation: Forkey :, si dice 'In genere questo viene implicitamente richiamata'. In altre parole, non dovresti chiamarlo direttamente. Si sta effettivamente destinato a fare la seguente, invece, che è ancora più facile l'impostazione da e per i valori, è sufficiente impostare i valori direttamente sullo strato:

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

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

Questo svanirà uno strato di opacità zero.

+0

Non lo vedo nei documenti, potrebbe essere una cosa OSX solo? Sto guardando CALayer [qui] (https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instm/CALayer/addAnimation: Forkey :). La maggior parte delle esercitazioni di Apple include una chiamata a "addAnimation: forKey:" ad es. [Qui] (https://developer.apple.com/library/ios/documentation/cocoa/conceptual/coreanimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html). – bcattle

3

Per impostare l'oggetto al termine dell'animazione basta scrivere il codice come questo

#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

non funziona per me –

1

Si può solo impostare removedOnCompletion su NO se si vuole preservare il valore finale. E non dimenticare di impostare la modalità di riempimento.

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