2010-09-17 12 views
13

ho avuto questo pezzo di codice im mio app:iOS 4.2: Flip immagine utilizzando blocchi animazioni

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

Ma l'uso di questo metodo è sconsigliato in iOS 4.0 e versioni successive, e dovrei usare transitionWithView:duration:options:animations:completion:

Non riesco a farlo funzionare correttamente. Qualcuno può aiutarmi? Thx!

risposta

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

Grazie mille! Funziona come un fascino. – FransGuelinckx

+0

@KennyTM: sei in grado di chiarire usando [NULL vs nil per i blocchi] (http://stackoverflow.com/questions/5766208/which-is-the-right-one-nil-or-null-to-mark- no-Objective-C-blocco)? – penfold

+6

@FransGuelinckx si prega di contrassegnare questa risposta come accettata se risolvesse il problema. –

3

ho fatto questa funzione in base al codice:

- (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; 
        }]; 
}