UIView,动画,完成块,UIAlertView

Posted

技术标签:

【中文标题】UIView,动画,完成块,UIAlertView【英文标题】:UIView, animate, completionblock, UIAlertView 【发布时间】:2013-10-23 00:41:30 【问题描述】:

我有一个带有登录屏幕的应用程序,其中包含一个登录按钮和一个注册按钮,如果我检测到我无法访问互联网,我会通过显示覆盖视图来隐藏这些按钮

 self.coveringview

现在我用这个视图的 alpha 做一些简单的动画,一旦完成,我希望弹出一个 alertview 来告诉用户发生了什么,在这段代码中你可以看到我通过发出警报来做到这一点。显示在 animatewithduration 的完成块中。

bahvior 不是我想要的,警报弹出太快而没有向用户显示我希望它显示的淡出,我该怎么办?

NetworkStatus netStatus = [reachability currentReachabilityStatus];
if(netStatus == NotReachable) 
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection"
                                                  message:@"Unable to connect to the server.  
     Please check your internet connection and try again."
                                                 delegate:self
                                        cancelButtonTitle:nil
                                        otherButtonTitles:@"OK", nil];
  [UIView animateWithDuration:1.0
                         animations:^
                                self.coveringView.alpha = 1.0;
                         completion:^(BOOL finished) 
                             // this is the problem here, I need to make this alert show up after  
                             the fade out occurs completely
                             [alert show];
                             self.coveringView.hidden = NO;
                         ];

else 
  [UIView animateWithDuration:1.0
                   animations:^
                      self.coveringView.alpha = 0.0;
                   completion:^(BOOL finished) 
                      self.coveringView.hidden = YES;
                   ];

【问题讨论】:

【参考方案1】:

从 Interface Builder 中删除您的 self.coveringView.hidden = YES 并仅设置 alpha = 0

NetworkStatus netStatus = [reachability currentReachabilityStatus];
if(netStatus == NotReachable) 
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection"
                                                  message:@"Unable to connect to the server.  
     Please check your internet connection and try again."
                                                 delegate:self
                                        cancelButtonTitle:nil
                                        otherButtonTitles:@"OK", nil];

    [UIView animateWithDuration:1.0
                     animations:^
                            self.coveringView.alpha = 1.0;
                     completion:^(BOOL finished) 
                         // this is the problem here, I need to make this alert show up after  the fade out occurs completely
                      if(finished)
                         [alert show];
                      
                     ];                     




else 
  [UIView animateWithDuration:1.0
                   animations:^
                      self.coveringView.alpha = 0.0;
                   completion:^(BOOL finished) 
                      if(finished)
                      self.coveringView.hidden = YES;
                      
                   ];

【讨论】:

还是不行,弹出太快,我觉得这里多线程可能有问题?? 还是不做,我觉得还得再拖延一下。 尝试在完成块内初始化alertview 你能帮我做最后一次尝试吗,我会在几分钟内编辑答案 让我们continue this discussion in chat【参考方案2】:

您不需要同时使用 setHidden 和 0.0 的 alpha 它们都会使视图不会拦截触摸。您的 hidden/notHidden 可能与 alpha 更改发生冲突。

尝试像这样简化:

NetworkStatus netStatus = [reachability currentReachabilityStatus];
if(netStatus == NotReachable) 
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection"
                                                  message:@"Unable to connect to the server.  
     Please check your internet connection and try again."
                                                 delegate:self
                                        cancelButtonTitle:nil
                                        otherButtonTitles:@"OK", nil];
  [UIView animateWithDuration:1.0
                         animations:^
                                self.coveringView.alpha = 1.0;
                         completion:^(BOOL finished) 
                             //I need to make this alert show up after the fade out occurs completely
                             [alert show];
                         ];

else 
  [UIView animateWithDuration:1.0
                   animations:^
                      self.coveringView.alpha = 0.0;
                   completion:^(BOOL finished) 
                   ];

【讨论】:

嗨,这是对另一个主题的简化,但没有解决最初的问题,警报仍然突然出现。

以上是关于UIView,动画,完成块,UIAlertView的主要内容,如果未能解决你的问题,请参考以下文章

UIView 动画完成块在其他动画开始时触发

重叠 UIView 动画取消第一个完成块

链式 UIView 动画,每个动画都包含循环内的完成块 [重复]

UIAlertView 和基于块的动画

UIView 页面卷曲动画不适用于块

UIVIew 块动画后用户交互中断