让UIView不断淡入淡出 - 完成:^ {可能无限循环?

Alb*_*haw 8 animation memory-management objective-c ios

我编写了以下代码,以使我的UIView不断淡入淡出.(FadeAlphaValue是BOOL)......

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

它有效,但我觉得如果我让它永远运行会导致一些奇怪的崩溃...我不熟悉[..^{..} completion^{...}];那种表示法.而且我觉得因为我在完成期间调用"淡入淡出"功能它直到"淡入淡出"功能完成才真正完成,问题是淡入淡出功能会在它完成之前再次调用自身等等,它似乎是一个无限循环......这会在几百次迭代后导致某种奇怪的多线程冻结吗?

Col*_*inE 14

更好的方法是使用UIViewAnimationOptionRepeat将无限期重复动画的选项.

[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)! 
Run Code Online (Sandbox Code Playgroud)

  • `选项:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse`使用您的代码! (2认同)