J'ai 10 UILabels
, peut-être quelques UIImageViews
. Je veux afficher tous ces éléments dans 1 UIView
avec une transition FadeOut & FadeIn lisse, l'un après l'autre.
Je sais que mettre des animations UIView
dans une boucle for ne fonctionnera pas car les animations sont asynchronouss et ne donneront pas l'effet UIView
. Donc la façon dont je ferais normalement ceci est de UIView
animation UIView
set. c'est-à-dire qu'une fois l'animation d'un élément terminée, commencez la suivante. Pour 3-4 éléments, le code semble correct. Ainsi –
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{ //do something with alpha here - first element } completion:^(BOOL finished){ [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{ //do something with alpha here - 2nd element} completion:^(BOOL finished){ ... } }
Mais pour 10 + éléments, il devient très salissant. Comment irait-on faire ça? Essentiellement, je crée un UIView
avec un contenu en boucle, un peu comme un widget.
Édité avec NSTimer
au lieu d'une boucle.
counter
est un ivar défini dans l'en-tête.
- (void)viewDidLoad { [super viewDidLoad]; counter = 0; [NSTimer scheduledTimerWithTimeInterval:0.30 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES]; } - (void) timerTick:(NSTimer *)timer{ UIView *currentView = [self.view.subviews objectAtIndex:counter]; [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{ currentView.alpha = 1.0;} completion:^(BOOL finished){ [UIView animateWithDuration:0.25 animations:^{currentView.alpha = 0.0;}]; } ]; counter++; if (counter >= [self.view.subviews count]) { counter = 0; } }