简体   繁体   中英

iOS 10 UILabels -> 1 UIView -> Loop through with Animation

I have 10 UILabels , maybe some UIImageViews . I want to display all these in 1 UIView with smooth FadeOut & FadeIn transition, one after the other.

I know putting UIView animations in a for-loop will not work as animations are done asynchronous & will not give proper effect. So the way normally I would do this is to chain UIView animation together. ie after one elements animation is complete begin the next one. For 3-4 elements the code looks ok. Like so -

[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){ ... }

                 }

But for 10+ elements it gets very messy. How would one go about doing this? Essentially I am creating a UIView with looping content, much like a widget.

Edited with NSTimer instead of a loop.

counter is an ivar defined in the header.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM