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?
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)!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With