8

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • This looks like infinite recursion, although I haven't tried it myself. There is another UIView animation method you should be using. – bneely Sep 25 '13 at 05:25
  • 1
    Your code is perfectly fine, and will not cause any problems. It's not technically recursion, because by the time the completion method fires, the previous invocation has long since returned. You could also use the auto-reverse approach mentioned in the duplicate question. – Duncan C Sep 29 '15 at 03:27

1 Answers1

14

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)! 
Shinnyx
  • 2,144
  • 2
  • 14
  • 21
ColinE
  • 68,894
  • 15
  • 164
  • 232
  • Nice! But how do I get it to fade in AND out? If it's repeating. Do I set the alpha equal to the absolute value of (the current alpha - 1)? Or something? I'll test! – Albert Renshaw Sep 25 '13 at 05:52
  • `fadeView.alpha = abs((int)fadeView.alpha-1);` didn't work! It fades out but then jumps back to alpha 1. – Albert Renshaw Sep 25 '13 at 05:54
  • I'm assuming the value gets set the first time through and then never updates again. – Albert Renshaw Sep 25 '13 at 05:54
  • Oh! But I was able to use the absolute value code in my original code to remove the need for a BOOL. – Albert Renshaw Sep 25 '13 at 05:56
  • 2
    `options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse` worked with your code! – Albert Renshaw Sep 25 '13 at 05:58
  • ****NOTE - this REPEAT animation STOPS if you exit the app... so you must call it again (and reset all animation variables (e.g. alpha) in the `UIApplicationDidBecomeActiveNotification` function as well! – Albert Renshaw Sep 25 '13 at 06:17