2010-09-17 5 views
13

J'ai eu ce morceau de code im mon application:iOS 4.2: Retourner l'image à l'aide d'animations bloc

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:1]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:imgView cache:YES]; 
    imgView.image = img2; 
[UIView commitAnimations]; 

Mais l'utilisation de cette méthode est déconseillée dans iOS 4.0 et versions ultérieures, et je devrais utiliser transitionWithView:duration:options:animations:completion:

Je ne peux pas obtenir cela pour fonctionner correctement. Quelqu'un peut-il m'aider? Thx!

Répondre

19
[UIView transitionWithView:imgView // use the forView: argument 
        duration:1   // use the setAnimationDuration: argument 
        options:UIViewAnimationOptionTransitionFlipFromLeft 
          // check UIViewAnimationOptions for what options you can use 
       animations:^{   // put the animation block here 
           imgView.image = img2; 
          } 
       completion:NULL];  // nothing to do after animation ends. 
+0

Merci beaucoup! Fonctionne comme un charme. – FransGuelinckx

+0

@KennyTM: êtes-vous capable de clarifier en utilisant [NULL vs nil pour les blocs] (http://stackoverflow.com/questions/5766208/which-is-the-right-one-nil-or-null-to-mark- no-objective-c-block)? – penfold

+6

@FransGuelinckx s'il vous plaît marquer cette réponse comme acceptée si elle a résolu votre problème. –

3

J'ai fait cette fonction en fonction de votre code:

- (void)flipCurrentView:(UIView*)oldView withNewView:(UIView*)newView reverse:(BOOL)reverse 
{ 
    newView.alpha = 0; 
    [UIView transitionWithView:newView 
         duration:1  
         options:UIViewAnimationOptionTransitionFlipFromLeft 
        animations:^{   
         oldView.alpha = 0; 
         newView.alpha = 1; 
        } 
        completion:^(BOOL finished){ 
         if(reverse){ 
          [self flipCurrentView:newView withNewView:oldView reverse:NO]; 
         } 
         finished = YES; 
        }]; 
} 
Questions connexes