2011-12-29 48 views
0

我一直在尽量减少我的应用程序中的所有功能,但无法真正找到如何使这个功能更好,也许有人比我更好? :)我该如何使这段代码更漂亮/更好/更少线条?

-(void)showRivBoxWithAnimtation:(BOOL)yesno { 
    if(yesno) { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.2]; 
     if ([self alpha] > 0) { 
      [self setAlpha:0.0]; 
      [appDelegate.JSONparser setDelegate:self.delegate]; //Give back the JSONparser to the parent! 
     } else { 
      [self setAlpha:1.0]; 
     } 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDidStopSelector:@selector(clearRivBoxContent:finished:context:)]; 
     [UIView commitAnimations]; 
    } else { 
     if ([self alpha] > 0) { 
      [self setAlpha:0.0]; 
      [appDelegate.JSONparser setDelegate:self.delegate]; //Give back the JSONparser to the parent! 
     } else { 
      [self setAlpha:1.0]; 
     } 
    } 
} 
+1

属于上http://codereview.stackexchange.com? – 2011-12-29 12:15:53

回答

2

这是我会做:

-(void)showRivBoxWithAnimtation:(BOOL)yesno { 
    [UIView animateWithDuration:yesno ? 0.2 : 0.0 
        animations:^{ 
     if ([self alpha] > 0) { 
      [self setAlpha:0.0]; 
      [appDelegate.JSONparser setDelegate:self.delegate]; //Give back the JSONparser to the parent! 
     } else { 
      [self setAlpha:1.0]; 
     } 
     } 
        completion:^(BOOL finished){ 
     if (finished) { 
      // Do the stuff from clearRivBoxContent:finished:context: 
     } 
    }]; 
} 
+0

好东西在那里:) – Hjalmar 2011-12-29 12:15:57

+0

这比我的回答更好。 – 2011-12-29 12:16:38

0

除非操作的顺序wrt。 alpha是至关重要的;这可能是最小的。

-(void)showRivBoxWithAnimtation:(BOOL)yesno { 
    if ([self alpha] > 0) { 
    [self setAlpha:0.0]; 
    [appDelegate.JSONparser setDelegate:self.delegate]; //Give back the JSONparser to the parent! 
    } else { 
    [self setAlpha:1.0]; 
    } 

    if(yesno) { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.2]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(clearRivBoxContent:finished:context:)]; 
    [UIView commitAnimations]; 
    } 
}