2010-09-17 45 views
13

我有這一塊代碼的IM我的應用程序:的iOS 4.2:翻轉圖像使用塊動畫

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

但使用這種方法在iOS 4.0的勸阻,後來,我應該使用transitionWithView:duration:options:animations:completion:

我不能讓這個工作正常。誰能幫我? Thx!

回答

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

非常感謝!奇蹟般有效。 – FransGuelinckx 2010-09-19 10:35:17

+0

@KennyTM:你能夠澄清使用[NULL vs nil for blocks](http://stackoverflow.com/questions/5766208/which-is-the-right-one-nil-or-null-to-mark-無目標c塊)? – penfold 2011-10-02 08:12:23

+6

@FransGuelinckx請將此答案標記爲已接受,如果它解決了您的問題。 – 2011-12-20 13:41:42

3

我做了這個功能,根據您的代碼:

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