Loading an “overlay” when running long tasks in iOS

前端 未结 11 952
没有蜡笔的小新
没有蜡笔的小新 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 17:02

    Swift 3.

    I used @Lucho's code in his answer below and I changed the overlay background color to clear and added a spinner color.

    public class LoadingOverlay {
    
        var overlayView : UIView!
        var activityIndicator : UIActivityIndicatorView!
    
        class var shared: LoadingOverlay {
            struct Static {
                static let instance: LoadingOverlay = LoadingOverlay()
            }
            return Static.instance
        }
    
        init(){
            self.overlayView = UIView()
            self.activityIndicator = UIActivityIndicatorView()
    
            overlayView.frame = CGRect(0, 0, 80, 80)
            overlayView.backgroundColor = .clear
            overlayView.clipsToBounds = true
            overlayView.layer.cornerRadius = 10
            overlayView.layer.zPosition = 1
    
            activityIndicator.frame = CGRect(0, 0, 40, 40)
            activityIndicator.center = CGPoint(overlayView.bounds.width / 2, overlayView.bounds.height / 2)
            activityIndicator.activityIndicatorViewStyle = .whiteLarge
            activityIndicator.color = .gray
            overlayView.addSubview(activityIndicator)
        }
    
        public func showOverlay(view: UIView) {
            overlayView.center = view.center
            view.addSubview(overlayView)
            activityIndicator.startAnimating()
        }
    
        public func hideOverlayView() {
            activityIndicator.stopAnimating()
            overlayView.removeFromSuperview()
        }
    }
    

提交回复
热议问题