本文介绍了从Swift中的任何类加载屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图快速实现加载屏幕,以便可以从项目中的任何类中重用(我有一些类可以处理长期运行"的活动)

I am trying to implement a loading screen in swift, that I can reuse from any class in the project (I have a few classes that will handle 'long running' activities)

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

The public function is, from another answer:

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(位于它所在的位置)中可以正常工作

Which works fine in FirstViewController.swift (Where it is located) by using:

LoadingOverlay.shared.showOverlay(self.view)

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

My question is, how do I use it in XYZ.swift? As self.view may not be referring to a view generated by that class. Is it possible to call and find the current super view, then add the loading screen on top of it?

推荐答案

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

You need to add your overlay to your main UIWindow class. In that case you will overlay all views.

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或其他内容.

PS: you can move most of overlayView customization code to init function, because you don't need to update backgroundColor or other things every time on display.

PPS:有许多很棒的库可用于加载

PPS: There is great libraries for loading overlay like

https://github.com/TransitApp/SVProgressHUD

https://github.com/jdg/MBProgressHUD

和其他.只是谷歌它

这篇关于从Swift中的任何类加载屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 07:46