Как я могу сделать этот фрагмент красивее / лучше / меньше строк? - PullRequest
0 голосов
/ 29 декабря 2011

Я пытался свести к минимуму все свои функции в своем приложении, но не могу найти, как сделать эту функцию лучше, может, кто-то намного лучше в этом, чем я?:)

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

Ответы [ 2 ]

2 голосов
/ 29 декабря 2011

Вот что я бы сделал:

-(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 голосов
/ 29 декабря 2011

Если не указан порядок операцийальфа имеет решающее значение;это, вероятно, самое маленькое, что может быть сделано.

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