performSelector is calling new in if loop

80 views Asked by At

i have

if (_moves <=19) {

    [self performSelector:@selector(changeLabelState:) withObject:nil afterDelay: 1.0];

}

And the Method:

- (void)changeLabelState:(NSTimer *)timer
{

[UIView transitionWithView:self.movesLeftLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    self.movesLeftLabel.textColor = [UIColor whiteColor];
} completion:^(BOOL finished) {

    [UIView transitionWithView:self.movesLeftLabel duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.movesLeftLabel.textColor = [UIColor darkGrayColor];
    } completion:^(BOOL finished) {
    }];
}];
[self performSelector:@selector(changeLabelState:) withObject:nil afterDelay:1.5];
}

So if _moves = 19, everything works fine. its changing to white and back to grey. but if the _moves = 18, it calls another time, and the transition is too fast.

how can i do it that he is not calling it another time?

1

There are 1 answers

0
Doro On

If you wrote you code in loop, and you do not want to perform selector more than one time try to add 'break;' after perform

if (_moves <=19) {

    [self performSelector:@selector(changeLabelState:) withObject:nil afterDelay: 1.0];
     break;
}

or, if you want, change clause to

if (_moves ==19) {

    [self performSelector:@selector(changeLabelState:) withObject:nil afterDelay: 1.0];

}