答案 0 :(得分:3)
@ Gordonium在Swift课程中的答案
public class LoaderOverlay {
private var loaderOverlayView: UIView!
class var shared: LoaderOverlay {
struct Static {
static let instance: LoaderOverlay = LoaderOverlay()
}
return Static.instance
}
private func setup() {
loaderOverlayView = UIView(frame: CGRectMake(0, 0, 110, 100))
loaderOverlayView.backgroundColor = UIColor.blackColor()
loaderOverlayView.alpha = 0.8
loaderOverlayView.layer.cornerRadius = 8
let indicator: UIActivityIndicatorView = UIActivityIndicatorView.init(activityIndicatorStyle: .White)
indicator.tag = 1
loaderOverlayView.addSubview(indicator)
indicator.center = CGPointMake(CGRectGetWidth(loaderOverlayView.frame)/2.0, CGRectGetHeight(loaderOverlayView.frame)/2.0)
indicator.startAnimating()
}
// MARK:- Public
public func show() {
if loaderOverlayView == nil {
self.setup()
} else {
let indicator = loaderOverlayView.viewWithTag(1) as! UIActivityIndicatorView
indicator.startAnimating()
}
if let window = UIApplication.sharedApplication().delegate?.window {
window!.addSubview(loaderOverlayView)
loaderOverlayView.center = window!.center
}
}
public func hide() {
let indicator = loaderOverlayView.viewWithTag(1) as! UIActivityIndicatorView
indicator.stopAnimating()
loaderOverlayView.removeFromSuperview()
}
}
使用方法: 显示
LoaderOverlay.shared.show()
隐藏
LoaderOverlay.shared.hide()
答案 1 :(得分:1)
这是Obj-C而不是Swift,但我有一个旧项目,里面有这样的东西。用户界面不一样,但我确信很容易改成你想要的东西。它在深色背景上添加了一个活动指示器(旋转器)而不是刻度线。您可以从任何地方调用此代码,并在将其添加到主窗口时显示。如果您只在一个地方需要它,那么只需将其添加到呈现视图中即可。
如果要显示它,请调用此代码:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 100)];
myView.backgroundColor = [UIColor blackColor];
myView.alpha = 0.8;
myView.layer.cornerRadius = 8;
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[myView addSubview:act];
act.center = CGPointMake(CGRectGetWidth(myView.frame)/2.0, CGRectGetHeight(myView.frame)/2.0);
[act startAnimating];
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
[mainWindow addSubview:myView];
myView.center = mainWindow.center;