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



我编写了以下代码来使我的UIView不断淡入淡出。(FadeAlphaValue 是一个布尔值)...

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

有效,但我有一种感觉,如果我让它永远运行,它会导致一些奇怪的崩溃......我不熟悉[..^{..} completion^{...}];这种符号。而且我觉得由于我在完成过程中调用"淡入淡出"函数,因此在"淡入淡出"函数完成之前它实际上不会完成,问题是淡入淡出函数会在完成之前再次调用自己等等,这似乎是一个无限循环......这会导致几百次迭代后出现某种奇怪的多线程冻结吗?

更好的方法是使用 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)! 

最新更新