Loading an “overlay” when running long tasks in iOS

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

    For anyone late like me, I made some modifications to @Sonrobby code. As i understand, @Sonrobby adds the activity to the overlay on every showOverlay call. And some of the configuration can be passed to the init function, letting only the placement on the showOverlay method.

    I also change the overlay's background to black, since my app it is mostly white.

    here is the code :

    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 = CGRectMake(0, 0, 80, 80)
            overlayView.backgroundColor = UIColor(white: 0, alpha: 0.7)
            overlayView.clipsToBounds = true
            overlayView.layer.cornerRadius = 10
            overlayView.layer.zPosition = 1
    
            activityIndicator.frame = CGRectMake(0, 0, 40, 40)
            activityIndicator.center = CGPointMake(overlayView.bounds.width / 2, overlayView.bounds.height / 2)
            activityIndicator.activityIndicatorViewStyle = .WhiteLarge
            overlayView.addSubview(activityIndicator)
        }
    
        public func showOverlay(view: UIView) {
            overlayView.center = view.center
            view.addSubview(overlayView)
            activityIndicator.startAnimating()
        }
    
        public func hideOverlayView() {
            activityIndicator.stopAnimating()
            overlayView.removeFromSuperview()
        }
    }
    

提交回复
热议问题