【问题标题】:Disable all Input while UIActivityIndicatorView spinningUIActivityIndi​​catorView 旋转时禁用所有输入
【发布时间】:2012-03-04 11:43:46
【问题描述】:

如何在UIActivityIndicatorView 旋转时禁用所有输入?

谢谢

【问题讨论】:

  • 如果您将微调器添加到 UIAlertView 然后显示警报,那么这将实现您所追求的。
  • 您也可以使用MBProgressHUD 获得不错的效果,但根据您想要达到的效果,这可能会更重。
  • 感谢 Luke,如果我想让 UIAlertView 消失,我该如何“销毁”它。有 0 个按钮可以吗?
  • 没有按钮也很好。只需调用 [someAlertView dismissWithClickedButtonIndex:0 animated:YES];
  • 请看这个链接【有关于这个话题的详细对话】[1] [1]:stackoverflow.com/questions/5404856/…

标签: ios cocoa-touch uiactivityindicatorview


【解决方案1】:

你可以在启动微调器时调用beginIgnoringInteractionEvents

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

endIgnoringInteractionEvents 当你停止它时。

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

只需确保您的代码始终到达您调用endIgnoringInteractionEvents 的位置,否则您的应用将冻结(从用户的角度来看)。

【讨论】:

  • 谢谢,工作正常。但正如我所看到的,视图确实“记住”了一次触摸并在 endIgnoreInteractionsEvent 之后触发事件。这种行为可以改变吗?
  • @mica:哇,这是一个有趣的观察——以前没有注意到这一点。我会看看。如果你赶时间,你可以劫持应用程序的主窗口sendEvent:,然后决定(即通过检查某种标志是否微调器正在动画)你是否将事件发送到响应者链或忽略它。劫持此窗口的一种方法是hereMyKindOfWindow 定义的答案)。
  • @mica:看起来你发现了一个错误。根据documentation,这不应该发生:“在一段时间内关闭触摸事件的传递。应用程序可以调用 UIApplication 方法 beginIgnoringInteractionEvents 并稍后调用 endIgnoringInteractionEvents 方法。第一个方法完全停止应用程序接收触摸事件;调用第二种方法来恢复接收此类事件。
  • 感谢您的调查。在这种特殊情况下,这种行为对我来说是可以的。如果没有,我想我会在 UIActivityIndi​​cator 旋转时将透明视图置于所有其他视图之上。
  • 谢谢那位帮助!!
【解决方案2】:

在 Swift 3.0 中:

禁用交互:

UIApplication.shared.beginIgnoringInteractionEvents() 

恢复交互:

UIApplication.shared.endIgnoringInteractionEvents() 

【讨论】:

    【解决方案3】:

    只是对 rokjarc 答案的补充。 这是一个让应用程序保持活力的看门狗示例。 你总是可以用一些关键的时间间隔打电话,也许是 10 秒。 如果您需要在 10 秒内启用,只需调用“启用”方法即可。

    UIWindow * __weak mainWindow;
    
    - (void)disableGlobalUserInteractionForTimeInterval:(NSTimeInterval)interval
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            mainWindow = [[[UIApplication sharedApplication] windows] lastObject];
        });
    
        [mainWindow setUserInteractionEnabled:false];
    
        if (interval > 0)
        {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self enableGlobalUserInteraction];
            });
        }
    }
    
    - (void)enableGlobalUserInteraction
    {
        if (mainWindow)
        {
            [mainWindow setUserInteractionEnabled:true];
        }
    }
    

    【讨论】:

      【解决方案4】:

      在 Swift 5 中:

      // activity indicator starts
      view.isUserInteractionEnabled = false
      
      ...
      
      // activity indicator stops
      view.isUserInteractionDisabled = true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多