【问题标题】:Make UIView constantly fade in and out - completion:^{ possible infinite loop? [duplicate]使 UIView 不断淡入淡出 - 完成:^{ 可能无限循环? [复制]
【发布时间】:2013-09-30 13:32:30
【问题描述】:

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

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

它有效,但我感觉如果我让它永远运行它会导致一些奇怪的崩溃......我不熟悉 [..^{..} completion^{...}]; 那个符号。而且我觉得因为我在完成过程中调用了“fade”函数,所以直到“fade”函数完成它才会真正完成,问题是fade函数会在完成之前再次调用自己,依此类推,这似乎是一个无限循环......这是否会在数百次迭代后导致某种奇怪的多线程冻结?

【问题讨论】:

  • 这看起来像无限递归,虽然我自己没有尝试过。您应该使用另一种 UIView 动画方法。
  • 您的代码非常好,不会造成任何问题。从技术上讲,这不是递归,因为当完成方法触发时,之前的调用早已返回。您还可以使用重复问题中提到的自动反转方法。

标签: ios objective-c animation memory-management


【解决方案1】:

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

【讨论】:

  • 不错!但是我如何让它淡入淡出呢?如果是重复的话。我是否将 alpha 设置为等于(当前 alpha - 1)的绝对值?或者其他的东西?我会测试!
  • fadeView.alpha = abs((int)fadeView.alpha-1); 没用!它会淡出,然后跳回 alpha 1。
  • 我假设该值是第一次设置,然后再也不会更新。
  • 哦!但是我能够在原始代码中使用绝对值代码来消除对 BOOL 的需求。
  • options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse 使用您的代码!
猜你喜欢
  • 2016-10-19
  • 2021-03-07
  • 1970-01-01
  • 2012-09-29
  • 2013-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
相关资源
最近更新 更多