2011-06-13 4 views
7

J'essaie d'apprendre l'animation de base pour l'iOS/iPhone. Ma couche racine contient beaucoup de sous-couches (sprites) et elles devraient tourner quand elles sont retirées ...Comment supprimer un objet CALayer de animationDidStop?

Mon plan consistait à ajouter une animation tournante, puis à supprimer l'image-objet lorsque l'animationDidStop est invoquée. Le problème est que la couche de sprite n'est pas un paramètre de animationDidStop!

Quelle est la meilleure façon de trouver la couche d'image-objet spécifique à partir de animationDidStop? Y a-t-il une meilleure façon de faire tourner le sprite quand il est retiré? (Idéalement, je voudrais utiliser kCAOnOrderOut mais je ne pouvais pas le faire fonctionner)

-(void) eraseSprite:(CALayer*)spriteLayer { 
    CABasicAnimation* animSpin = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
    animSpin.toValue = [NSNumber numberWithFloat:2*M_PI]; 
    animSpin.duration = 1; 
    animSpin.delegate = self; 
    [spriteLayer addAnimation:animSpin forKey:@"eraseAnimation"];  
} 



- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{ 
    // TODO check if it is an eraseAnimation 
    //  and find the spriteLayer 

    CALayer* spriteLayer = ?????? 
    [spriteLayer removeFromSuperlayer]; 
} 

Répondre

23

trouvé cette réponse ici cocoabuilder mais, fondamentalement, vous ajoutez une valeur clé du CABasicAnimation pour CALayer qui est en cours d'animation.

- (CABasicAnimation *)animationForLayer:(CALayer *)layer 
{ 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
    /* animation properties */ 
    [animation setValue:layer forKey:@"animationLayer"]; 
    [animation setDelegate:self]; 
    return animation; 
} 

référence Ensuite, dans le rappel animationDidStop

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 
{ 
    CALayer *layer = [anim valueForKey:@"animationLayer"]; 
    if (layer) { 
     NSLog(@"removed %@ (%@) from superview", layer, [layer name]); 
     [layer removeFromSuperlayer]; 
    } 
} 
+1

Cela devrait être la réponse, car celle acceptée ne fonctionne pas sur plusieurs sprites comme la question l'indique. – Ian1971

+0

+1 réponse correcte ... – Leena

+0

Bon, j'ai maintenant vérifié cette réponse comme la bonne. – ragnarius

0

Vous pourriez avoir une iVariTempSpriteLayer de type `CALayer.

@property (nonautomic, assign) CALayer* iTempSpriteLayer; 

-(void) eraseSprite:(CALayer*)spriteLayer { 

    iTempSpriteLayer = spriteLayer; 
    ........................... 
} 


- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{ 
    // TODO check if it is an eraseAnimation 
    //  and find the spriteLayer 
    if(iTempSpriteLayer) 
     [iTempSpriteLayer removeFromSuperlayer]; 
    iTempSpriteLayer = nil; 
} 
+0

+1, Merci, mais j'ai beaucoup sprites et ils risquent d'être supprimés en même temps (en chevauchement des délais). – ragnarius

+0

Je crois que la réponse de Nate ci-dessous est la bonne. – Ian1971

Questions connexes