2012-07-20 5 views
0

Salut je crée une application qui a une animation qui joue après un mouvement et puis il devient vide, j'ai besoin d'un moyen d'indiquer dans le code lorsque l'animation se termine jouer son, j'ai le code son, donc je dois juste savoir comment l'énoncer, j'utilise le code tel que [animation startanimating] et [animation stopanimating], [audioplayer play] et [audioplayer stop]. Merci je suis un débutant alors s'il vous plaît allez-y facile :)Comment indiquer la fin de l'animation?

+0

Postez votre code complet s'il vous plaît. –

+0

http://pastebin.com/x9m9qXcb – user1483652

+0

Qu'essayez-vous exactement d'accomplir avec l'animation? Voulez-vous parcourir une image toutes les secondes? –

Répondre

1

Vous pouvez convertir votre code pour utiliser un bloc avec un rappel de fin. Exemples dans this réponse.


modifier par exemple:

Disons que la vue que vous souhaitez animer est appelé myView

[UIView animateWithDuration:0.5 
        delay:0.0 
       options:UIViewAnimationOptionBeginFromCurrentState 
      animations:^{ 
       myView.alpha = 0; // a fade out animation 
      } 
      completion:^(BOOL finished) 
      { 
       [audioPlayer play]; 
       // whatever else 
      } 

]; 

Est-ce que vous avez essayé? Pouvez-vous poster plus de code réel? Cela aide à voir comment vous gérez votre animation et rappel dans son intégralité.

+0

Hey, Comme je l'ai dit, je suis un débutant total et je essayé de le faire mais cela n'a pas fonctionné, probablement parce que je ne le fais pas bien? – user1483652

+0

Voici comment je l'ai fait:^(BOOL terminé) { [lecteur audio]; play.hidden = 0; }; – user1483652

+0

Ok je vais créer une corbeille de collage et mettre le lien ici dans quelques minutes, il aura tout le code :) – user1483652

0
[UIView animateWithDuration:0.5 
         delay:0.0 
        options:UIViewAnimationOptionBeginFromCurrentState 
       animations:^{ 
        //animation block 
        // perform animation here 
       } 
       completion:^(BOOL finished){ 
        // play sound here 
        [audioPlayer play] 

    }]; 
+0

Devrais-je mettre cela dans mon code pour l'animation? – user1483652

+0

ce code est pour l'animation ... alors écrivez-le quand vous voulez effectuer l'animation ... –

0

Vous allez vouloir vouloir utiliser un CAKeyFrameAnimation. Cela vous permettra d'animer à travers une série d'images avec des temps d'image individuels et obtenir une notification de délégué lorsque vous avez atteint la fin de votre animation, quelque chose que l'animation avec un UIImageView ne fournira pas.

Quelques exemples de code pour vous aider à démarrer (ARC suppose):

// create an animation overlay view to display and animate our image frames 
UIView *animationView = [[UIView alloc] initWithFrame:frame]; 
[self.view addSubview:animationView]; 

// using 4 images for the example 
NSArray *values = [NSArray arrayWithObjects:(id)[[UIImage imageNamed:@"Image1.png"] CGImage], 
         (id)[[UIImage imageNamed:@"Image2.png"] CGImage], 
         (id)[[UIImage imageNamed:@"Image3.png"] CGImage], 
         (id)[[UIImage imageNamed:@"Image4.png"] CGImage], 
         nil]; 

// change the times as you need 
NSArray *keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], 
         [NSNumber numberWithFloat:0.25], 
         [NSNumber numberWithFloat:0.50], 
         [NSNumber numberWithFloat:1.0],nil]; 

// animating the contents property of the layer 
CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animationWithKeyPath:@"contents"]; 
keyframe.values = values; 
keyframe.keyTimes = keyTimes; 
keyframe.calculationMode = kCAAnimationLinear; 
keyframe.duration = 1.0; // 1 second, can be whatever you want, obviously 
keyframe.delegate = self; 
keyframe.removedOnCompletion = NO; 
keyframe.fillMode = kCAFillModeForwards; 

// "user defined string" can be whatever you want and probably should be constant somewhere in 
// your source. You can use this same key if you want to remove the animation 
// later using -[CALayer removeAnimationForKey:] on the animationView's layer 
[animationView.layer addAnimation:keyframe forKey:@"user defined string"]; 

Puis, quelque part ailleurs, mettre en œuvre votre méthode déléguée:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finished 
{ 
    if (finished) { 
     // play music 
    } 
} 
Questions connexes