Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make UIView constantly fade in and out - completion:^{ possible infinite loop? [duplicate]

I've written the following code to make my UIView constantly fade in and out. (FadeAlphaValue is a BOOL)...

-(void) fade {
    [UIView animateWithDuration:1.0
                     animations:^{
                         fadeView.alpha = (int)fadeAlphaValue;
                     }
                     completion:^(BOOL finished){
                         fadeAlphaValue=!fadeAlphaValue;
                         [self fade];
                     }];
}

It works but I have a feeling it's going to cause some weird crash if I let it run forever... I'm not to familiar with [..^{..} completion^{...}]; that notation. And I feel like since I'm calling the "fade" function during completion it won't actually complete until the "fade" function completes, the problem is the fade function will call itself again before it completes and so on and so on, it seems like an infinite loop... Is this going to cause some kind of weird multi-threading freeze after a few hundred iterations?

like image 210
Albert Renshaw Avatar asked Sep 25 '13 05:09

Albert Renshaw


1 Answers

A better approach to this is to use the UIViewAnimationOptionRepeat option which will repeat an animation indefinitely.

[UIView animateWithDuration:1.0f
                      delay:0.0f
                    options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                 animations:^{
                     // your animation code here
                 }
                completion:nil];//NOTE - this REPEAT animation STOPS if you exit the app (or viewcontroller)... so you must call it again (and reset all animation variables (e.g. alpha) in the UIApplicationDidBecomeActiveNotification function as well (or when the viewcontroller becomes active again)! 
like image 56
ColinE Avatar answered Oct 01 '22 16:10

ColinE