在iOS中运行长任务时加载"叠加"

Son*_*bby 61 overlay uiview ios swift

在执行长任务时,在Swift IOS应用程序中加载叠加层的示例是什么.从远程服务器加载数据的示例.我用谷歌搜索,但没有找到任何答案.

更新:

感谢@Sebastian Dressler这是一个简单的方法.我更新了我的代码并且运行很酷

public class LoadingOverlay{

var overlayView = UIView()
var activityIndicator = UIActivityIndicatorView()

class var shared: LoadingOverlay {
    struct Static {
        static let instance: LoadingOverlay = LoadingOverlay()
    }
    return Static.instance
}

    public func showOverlay(view: UIView) {

        overlayView.frame = CGRectMake(0, 0, 80, 80)
        overlayView.center = view.center
        overlayView.backgroundColor = UIColor(hex: 0x444444, alpha: 0.7)
        overlayView.clipsToBounds = true
        overlayView.layer.cornerRadius = 10

        activityIndicator.frame = CGRectMake(0, 0, 40, 40)
        activityIndicator.activityIndicatorViewStyle = .WhiteLarge
        activityIndicator.center = CGPointMake(overlayView.bounds.width / 2, overlayView.bounds.height / 2)

        overlayView.addSubview(activityIndicator)
        view.addSubview(overlayView)

        activityIndicator.startAnimating()
    }

    public func hideOverlayView() {
        activityIndicator.stopAnimating()
        overlayView.removeFromSuperview()
    }
}
Run Code Online (Sandbox Code Playgroud)

让我们使用:

LoadingOverlay.shared.showOverlay(self.view)
//To to long tasks
LoadingOverlay.shared.hideOverlayView()
Run Code Online (Sandbox Code Playgroud)

Aji*_*til 159

上面的答案添加了加载视图,但它不会阻止屏幕上的点击事件,也不会为屏幕的其余部分提供叠加.您可以按如下方式实现:

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .Alert)

alert.view.tintColor = UIColor.blackColor()
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(10, 5, 50, 50)) as UIActivityIndicatorView
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
loadingIndicator.startAnimating();

alert.view.addSubview(loadingIndicator)
presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

Swift 3.0

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)

let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating();

alert.view.addSubview(loadingIndicator)
present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

Swift 4.0

let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)

let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.gray
loadingIndicator.startAnimating();

alert.view.addSubview(loadingIndicator)
present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

你可以隐藏它如下:

dismiss(animated: false, completion: nil)
Run Code Online (Sandbox Code Playgroud)

它将显示如下: 在此输入图像描述

  • 在XCode 8.2中不存在dismissViewControllerAnimated(false,completion:nil).它已被更改为dismiss(动画:false,完成:nil) (4认同)
  • 一个更安全的关闭方法是:`internal func distinctAlert() { if let vc = self.presentedViewController, vc is UIAlertController {dismiss(animated: false, completion: nil) } }` (3认同)

Seb*_*ler 32

只需创建一个叠加视图,您可以将其添加到父视图中,并在完成任务后将其删除,例如添加它:

var overlay : UIView? // This should be a class variable

[ ... ]

overlay = UIView(frame: view.frame)
overlay!.backgroundColor = UIColor.blackColor()
overlay!.alpha = 0.8

view.addSubview(overlay!)
Run Code Online (Sandbox Code Playgroud)

删除:

overlay?.removeFromSuperview()
Run Code Online (Sandbox Code Playgroud)


Mao*_*aor 23

模糊背景+活动指标,Swift 3示例

extension UIView {
    func showBlurLoader() {
        let blurLoader = BlurLoader(frame: frame)
        self.addSubview(blurLoader)
    }

    func removeBluerLoader() {
        if let blurLoader = subviews.first(where: { $0 is BlurLoader }) {
            blurLoader.removeFromSuperview()
        }
    }
}


class BlurLoader: UIView {

    var blurEffectView: UIVisualEffectView?

    override init(frame: CGRect) {
        let blurEffect = UIBlurEffect(style: .dark)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.frame = frame
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.blurEffectView = blurEffectView
        super.init(frame: frame)
        addSubview(blurEffectView)
        addLoader()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func addLoader() {
        guard let blurEffectView = blurEffectView else { return }
        let activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
        activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        blurEffectView.contentView.addSubview(activityIndicator)
        activityIndicator.center = blurEffectView.contentView.center
        activityIndicator.startAnimating()
    }
}
Run Code Online (Sandbox Code Playgroud)

演示


Luc*_*cho 8

对于像我这样迟到的人,我对@Sonrobby代码进行了一些修改.据我了解,@ Sonrobby在每次showOverlay通话时都将活动添加到叠加层.并且一些配置可以传递给init函数,只允许在showOverlay方法上放置.

我还将叠加层的背景更改为黑色,因为我的应用程序主要是白色.

这是代码:

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()
    }
}
Run Code Online (Sandbox Code Playgroud)


Muh*_*raf 7

@Ajinkya Patil 回答作为参考。Swift 4.0 及更高版本

这是一个可在所有 viewController 上使用而不会发生冲突的扩展解决方案。

创建一个LoadingDialog+ViewContoller.swift

import UIKit

struct ProgressDialog {
    static var alert = UIAlertController()
    static var progressView = UIProgressView()
    static var progressPoint : Float = 0{
        didSet{
            if(progressPoint == 1){
                ProgressDialog.alert.dismiss(animated: true, completion: nil)
            }
        }
    }
}
extension UIViewController{
   func LoadingStart(){
        ProgressDialog.alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
    
    let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
    loadingIndicator.hidesWhenStopped = true
    loadingIndicator.style = UIActivityIndicatorView.Style.gray
    loadingIndicator.startAnimating();

    ProgressDialog.alert.view.addSubview(loadingIndicator)
    present(ProgressDialog.alert, animated: true, completion: nil)
  }

  func LoadingStop(){
    ProgressDialog.alert.dismiss(animated: true, completion: nil)
  }
}
Run Code Online (Sandbox Code Playgroud)

在您喜欢的任何地方调用 ViewController 内的函数。像这样:

self.LoadingStart()
Run Code Online (Sandbox Code Playgroud)

以下是如何停止加载对话框。

self.LoadingStop()
Run Code Online (Sandbox Code Playgroud)


Rup*_*dra 6

使用 ATKit。

参考:https : //aurvan.github.io/atkit-ios-release/index.html

ATProgressOverlay 类 https://aurvan.github.io/atkit-ios-release/helpbook/Classes/ATProgressOverlay.html

代码:

import ATKit

ATProgressOverlay.sharedInstance.show() // Does not show network activity indicator on status bar.

ATProgressOverlay.sharedInstance.show(isNetworkActivity: true) // Shows network activity indicator on status bar.
Run Code Online (Sandbox Code Playgroud)

截屏:

加载叠加截图


Sho*_*tta 6

雨燕5

class func showUniversalLoadingView(_ show: Bool, loadingText : String = "") {
    let existingView = UIApplication.shared.windows[0].viewWithTag(1200)
    if show {
        if existingView != nil {
            return
        }
        let loadingView = self.makeLoadingView(withFrame: UIScreen.main.bounds, loadingText: loadingText)
        loadingView?.tag = 1200
        UIApplication.shared.windows[0].addSubview(loadingView!)
    } else {
        existingView?.removeFromSuperview()
    }

}



 class func makeLoadingView(withFrame frame: CGRect, loadingText text: String?) -> UIView? {
    let loadingView = UIView(frame: frame)
    loadingView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
    let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    //activityIndicator.backgroundColor = UIColor(red:0.16, green:0.17, blue:0.21, alpha:1)
    activityIndicator.layer.cornerRadius = 6
    activityIndicator.center = loadingView.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.style = .white
    activityIndicator.startAnimating()
    activityIndicator.tag = 100 // 100 for example

    loadingView.addSubview(activityIndicator)
    if !text!.isEmpty {
        let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
        let cpoint = CGPoint(x: activityIndicator.frame.origin.x + activityIndicator.frame.size.width / 2, y: activityIndicator.frame.origin.y + 80)
        lbl.center = cpoint
        lbl.textColor = UIColor.white
        lbl.textAlignment = .center
        lbl.text = text
        lbl.tag = 1234
        loadingView.addSubview(lbl)
    }
    return loadingView
}
Run Code Online (Sandbox Code Playgroud)

用途

showUniversalLoadingView(true, loadingText: "Downloading Data.......")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

显示UniversalLoadingView(true) 在此输入图像描述

移除装载机

showUniversalLoadingView(false)
Run Code Online (Sandbox Code Playgroud)


小智 5

要添加给出的答案,如果您有时尝试运行代码,则可能会遇到问题.就个人而言,有一个场合没有正确调用showOverlay(因为我试图进入场景,然后在viewDidLoad期间立即调用此函数).

如果遇到类似于我的问题,代码有一个修复,我建议改变方法.

FIX:将两个代码块作为闭包放到dispatch_async调用中,如下所示:

dispatch_async(dispatch_get_main_queue(),
 { //code });
Run Code Online (Sandbox Code Playgroud)

方法:在调用代码时,对主队列执行dispatch_after调用,将调用延迟几毫秒.

理由?你只是要求用户在viewDidLoad中做太多.

如果解决方案的这个附录有帮助,我会很高兴.

-Joel Long

PS Solution适用于XCode v6.3.2


ano*_*eal 5

更新了@sonrobby 答案,通过调整遮罩大小添加了背景视图和方向处理......这可以用于简单的东西

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你将它添加到 keywindow,它也可以通过你的导航栏和标签栏......像这样

LoadingOverlay.shared.showOverlay(view: UIApplication.shared.keyWindow!)
Run Code Online (Sandbox Code Playgroud)