Disable user interaction while loading in swift
Asked Answered
P

3

6

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()
            }
         }
    }
Partial answered 15/9, 2019 at 12:51 Comment(0)
E
18

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

and

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

Enantiomorph answered 29/11, 2019 at 16:13 Comment(3)
please provide more details for your answer.Pay
Is keyWindow property isUserInteractionEnabled good solution or not?Fireback
Damn, self.view.isUserInteractionEnabled does not disable navigation bar touches.Fireback
A
0

begin/end IgnoringInteractionEvents is now deprecated in iOS 13.0, use UIView's userInteractionEnabled property instead on your "myIndicatorView". And make your "myIndicatorView full screen so it covers the whole screen, that will allow you to block the whole app while it is visible.

Aminta answered 15/9, 2019 at 14:31 Comment(1)
But how? We already see this warning message in Xcode. Would you mind posting the code which makes it happen?Valerivaleria
F
0

IgnoringInteractionEvents is now deprecated in iOS 13.0 so You must use 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()
            }
Feeding answered 27/11, 2019 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.