Loading an “overlay” when running long tasks in iOS

前端 未结 11 943
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 16:15

What is example for loading overlay in Swift IOS application when do a long tasks. Example for loading data from remote server. I googled but not found any answer.

U

11条回答
  •  攒了一身酷
    2020-12-22 16:53

    Updated @sonrobby answer, added a background view and orientation handling via resizing mask... this can be used for simple stuffs

    public class LoadingOverlay{
    
        var overlayView = UIView()
        var activityIndicator = UIActivityIndicatorView()
        var bgView = UIView()
    
        class var shared: LoadingOverlay {
            struct Static {
                static let instance: LoadingOverlay = LoadingOverlay()
            }
            return Static.instance
        }
    
        public func showOverlay(view: UIView) {
    
            bgView.frame = view.frame
            bgView.backgroundColor = UIColor.gray
            bgView.addSubview(overlayView)
            bgView.autoresizingMask = [.flexibleLeftMargin,.flexibleTopMargin,.flexibleRightMargin,.flexibleBottomMargin,.flexibleHeight, .flexibleWidth]
            overlayView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
            overlayView.center = view.center
            overlayView.autoresizingMask = [.flexibleLeftMargin,.flexibleTopMargin,.flexibleRightMargin,.flexibleBottomMargin]
            overlayView.backgroundColor = UIColor.black
            overlayView.clipsToBounds = true
            overlayView.layer.cornerRadius = 10
    
            activityIndicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
            activityIndicator.activityIndicatorViewStyle = .whiteLarge
            activityIndicator.center = CGPoint(x: overlayView.bounds.width / 2, y: overlayView.bounds.height / 2)
    
            overlayView.addSubview(activityIndicator)
            view.addSubview(bgView)
            self.activityIndicator.startAnimating()
    
        }
    
        public func hideOverlayView() {
            activityIndicator.stopAnimating()
            bgView.removeFromSuperview()
        }
    }
    

    if you add it to keywindow, it can then go over your nav and tab bars also... something like this

    LoadingOverlay.shared.showOverlay(view: UIApplication.shared.keyWindow!)
    

提交回复
热议问题