首页 > 解决方案 > 快速加载时禁用用户交互

问题描述

我在 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()
            }
         }
    }

标签: iostouch-eventuiactivityindicatorviewswift4.2xcode10.1

解决方案


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

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


推荐阅读