Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable user interaction while loading in swift

I'm making an API call in my ViewController willAppear method and till the API response completes I'm using this showWait and hideWait method to show and dismiss the activity indicator. But the problem I have is, in my ViewController I have a table view with different custom cells and suppose I keep tapping on the cells during the loading all of its action is getting called multiple times once the loading disappears. Below is my code for showing and hiding indicator, I tried setting userInteractionEnabled to false but that didn't work. Also, tried the beginIgnoringInteractionEvents, it works to some extents but if you keep on tapping till the loader disappears this fails as well. Not sure how to proceed from here. Any help is appreciated.

 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()
            }
         }
    }
like image 669
Francis F Avatar asked Sep 15 '19 12:09

Francis F


1 Answers

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

and

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

like image 71
Eloy Avatar answered Oct 21 '22 18:10

Eloy