我编写了以下代码,以使我的UIView不断淡入淡出。 (FadeAlphaValue是BOOL)......
-(void) fade {
[UIView animateWithDuration:1.0
animations:^{
fadeView.alpha = (int)fadeAlphaValue;
}
completion:^(BOOL finished){
fadeAlphaValue=!fadeAlphaValue;
[self fade];
}];
}
它有效,但我觉得如果我让它永远运行会导致一些奇怪的崩溃......我不熟悉[..^{..} completion^{...}];
这种符号。而且我觉得因为我在完成期间调用“淡入淡出”功能它直到“淡入淡出”功能完成才真正完成,问题是淡入淡出功能会在它完成之前再次调用自身等等,它似乎是一个无限循环......这会在几百次迭代后导致某种奇怪的多线程冻结吗?
答案 0 :(得分: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)!