Ios 从Swift中的任何类加载屏幕

Ios 从Swift中的任何类加载屏幕,ios,swift,screen,loading,Ios,Swift,Screen,Loading,我正在尝试用swift实现一个加载屏幕,我可以从项目中的任何类重用它(我有几个类可以处理“长时间运行”的活动) 从另一个答案来看,公共职能是: public class LoadingOverlay{ var overlayView = UIView() var activityIndicator = UIActivityIndicatorView() class var shared: LoadingOverlay { struct Static { static

我正在尝试用swift实现一个加载屏幕,我可以从项目中的任何类重用它(我有几个类可以处理“长时间运行”的活动)

从另一个答案来看,公共职能是:

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 = MyGlobalVariables.UICOLORGREEN
    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()
}
}

在FirstViewController.swift(它所在的位置)中,通过使用以下各项可以正常工作:

LoadingOverlay.shared.showOverlay(self.view)

我的问题是,如何在XYZ.swift中使用它?As self.view可能不引用该类生成的视图。是否可以调用并找到当前的超级视图,然后在其顶部添加加载屏幕?

您需要将覆盖添加到主
UIWindow
类中。在这种情况下,将覆盖所有视图

public func showOverlay() {
    if  let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate,
        let window = appDelegate.window {
            overlayView.frame = CGRectMake(0, 0, 80, 80)
            overlayView.center = CGPointMake(window.frame.width / 2.0, window.frame.height / 2.0)
            overlayView.backgroundColor = MyGlobalVariables.UICOLORGREEN
            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)
            window.addSubview(overlayView)

            activityIndicator.startAnimating()
    }
}
PS:您可以将大部分
overlayView
自定义代码移动到
init
功能,因为您不需要每次更新
backgroundColor
或其他显示内容

PPS:有很好的库可以加载覆盖,比如


等等。只需在谷歌上搜索一下,你就可以将覆盖添加到你的主
ui窗口
类中。在这种情况下,将覆盖所有视图

public func showOverlay() {
    if  let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate,
        let window = appDelegate.window {
            overlayView.frame = CGRectMake(0, 0, 80, 80)
            overlayView.center = CGPointMake(window.frame.width / 2.0, window.frame.height / 2.0)
            overlayView.backgroundColor = MyGlobalVariables.UICOLORGREEN
            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)
            window.addSubview(overlayView)

            activityIndicator.startAnimating()
    }
}
PS:您可以将大部分
overlayView
自定义代码移动到
init
功能,因为您不需要每次更新
backgroundColor
或其他显示内容

PPS:有很好的库可以加载覆盖,比如


等等。只要用谷歌搜索一下就可以了。
static var shared=LoadingOverlay()
-更好的方法。
static var shared=LoadingOverlay()
-更好的方法。谢谢你。效果很好,但我无法删除视图。我需要调整hideOverlayView()吗?@Colin hum
hideOverlayView
应该可以正常工作,但不确定为什么在删除覆盖时出现问题请在此处找到:感谢您回来@科林:哦,我以为你在主线程中调用了
hideOverlayView
:)谢谢你。效果很好,但我无法删除视图。我需要调整hideOverlayView()吗?@Colin hum
hideOverlayView
应该可以正常工作,但不确定为什么在删除覆盖时出现问题请在此处找到:感谢您回来@科林:哦,我以为你在主线程中调用了
hideOverlayView
:-)