【问题标题】:Disable user interaction while loading in swift快速加载时禁用用户交互
【发布时间】:2020-01-16 13:09:55
【问题描述】:

我在 ViewController willAppear 方法中进行 API 调用,直到 API 响应完成,我正在使用这个 showWait 和 hideWait 方法来显示和关闭活动指示器。但是我遇到的问题是,在我的 ViewController 中,我有一个带有不同自定义单元格的表格视图,假设我在加载过程中不断点击单元格,一旦加载消失,它的所有操作都会被多次调用。下面是我显示和隐藏指示器的代码,我尝试将 userInteractionEnabled 设置为 false,但没有奏效。此外,尝试了 beginIgnoringInteractionEvents,它在一定程度上有效,但如果你继续点击直到加载器消失,这也会失败。不知道如何从这里开始。任何帮助表示赞赏。

 class func showWait() {
        DispatchQueue.main.async {
            UIApplication.shared.beginIgnoringInteractionEvents()
            if let appdelegate = UIApplication.shared.delegate as? AppDelegate,
                appdelegate.myIndicatorView == nil {
                appdelegate.myIndicatorView = MyIndicatorView(frame: UIScreen.main.bounds)
                appdelegate.window?.subviews[0].addSubview(appdelegate.myIndicatorView!)
            }
        }
    }

 class func hideWait() {
        if let appdelegate = UIApplication.shared.delegate as? AppDelegate,
            appdelegate.myIndicatorView != nil {
            DispatchQueue.main.async {
                appdelegate.myIndicatorView?.removeFromSuperview()
                appdelegate.myIndicatorView = nil
                UIApplication.shared.endIgnoringInteractionEvents()
            }
         }
    }

【问题讨论】:

    标签: ios touch-event uiactivityindicatorview swift4.2 xcode10.1


    【解决方案1】:

    替换:UIApplication.shared.beginIgnoringInteractionEvents() ->>> self.view.isUserInteractionEnabled = false

    UIApplication.shared.endIgnoringInteractionEvents() ->>> self.view.isUserInteractionEnabled = true

    【讨论】:

    • 请提供更详细的答案。
    【解决方案2】:

    IgnoringInteractionEvents 现在在 iOS 13.0 中已弃用 所以你必须使用 isUserInteractionEnabled

        DispatchQueue.main.async {
                    appdelegate.myIndicatorView?.removeFromSuperview()
                    appdelegate.myIndicatorView = nil
                   //you must use this
                   self.view.isUserInteractionEnabled = false
    //This is deprecated in iOS13
    //             UIApplication.shared.endIgnoringInteractionEvents()
                }
    

    【讨论】:

      【解决方案3】:

      begin/end IgnoringInteractionEvents 现在在 iOS 13.0 中已弃用,请在“myIndi​​catorView”上使用 UIView 的 userInteractionEnabled 属性。并让您的“myIndi​​catorView 全屏显示,使其覆盖整个屏幕,这样您就可以在可见时阻止整个应用程序。

      【讨论】:

      • 但是怎么做呢?我们已经在 Xcode 中看到了这个警告信息。您介意发布实现它的代码吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多