Created
March 17, 2018 14:59
-
-
Save ryantxr/43dfc7441e56ad950b913aa17598d94b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a wait dialog to show "please wait" while the app is doing some | |
long running task. | |
To call it from a view controller: | |
WaitDialog.show(self) | |
// start long running task | |
// Long running task is done. | |
WaitDialog.hide() | |
*/ | |
import UIKit | |
open class WaitDialog { | |
static let sharedInstance = WaitDialog() | |
open static func show(_ parent: UIViewController, message: String? = nil) { | |
sharedInstance.showWaitDialog(parent.view) | |
} | |
open static func show(_ parent: UIView, message: String? = nil) { | |
sharedInstance.showWaitDialog(parent) | |
} | |
open static func hide() { | |
sharedInstance.hideWaitDialog() | |
} | |
var isWaiting: Bool = false | |
let view: PleaseWaitView = { | |
let width = 180 | |
let screen = UIScreen.main | |
let x = Int(screen.bounds.width) / 2 - (width / 2) | |
return PleaseWaitView(frame: CGRect(x: CGFloat(x), y: CGFloat(200), width: CGFloat(width), height: CGFloat(100))) | |
}() | |
open func showWaitDialog(_ parent: UIView, message: String? = nil) { | |
isWaiting = true | |
self.view.show(parent, customMessage: message) | |
} | |
open func hideWaitDialog() { | |
isWaiting = false | |
self.view.close() | |
} | |
} | |
class PleaseWaitView: UIView { | |
var messageLabel = UILabel() | |
var spinner = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray) | |
init() { | |
super.init(frame: CGRect(x: 100, y: 200, width: 100, height: 100)) | |
self.initDefaults() | |
} | |
override init(frame:CGRect){ | |
super.init(frame: frame) | |
self.initDefaults() | |
} | |
convenience init(frame: CGRect, message:String) { | |
self.init(frame: frame) | |
self.messageLabel.text = message | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func initDefaults() { | |
// self.backgroundColor = UIColor(red:249.0/255.0, green:249.0/255.0, blue:249.0/255.0, alpha:1.0) | |
self.backgroundColor = UIColor(red:25.0/255.0, green:25.0/255.0, blue:25.0/255.0, alpha:0.6) | |
self.layer.cornerRadius = 5 | |
self.layer.borderWidth = 1 | |
self.layer.borderColor = UIColor(red:10.0/255.0, green:10.0/255.0, blue:10.0/255.0, alpha:0.8).cgColor | |
} | |
func show(_ superview: UIView, customMessage: String? = nil) { | |
// Override the message if it was sent | |
if let customMessage = customMessage { | |
self.messageLabel.text = customMessage | |
} | |
else { | |
self.messageLabel.text = "Please wait" | |
} | |
// Add ourself to the super view. | |
superview.addSubview(self) | |
// Figure out where things go. | |
let width = self.frame.size.width | |
let x = Int(self.frame.size.width/2) - (Int(width) / 2) | |
let y = 8 | |
let height = CGFloat(24) | |
self.messageLabel.frame = CGRect(x: CGFloat(x), y: CGFloat(y), width: width, height: height) | |
self.messageLabel.textAlignment = NSTextAlignment.center | |
self.messageLabel.textColor = UIColor.white | |
self.addSubview(messageLabel) | |
spinner.center = CGPoint(x: self.frame.width/2, y: 65.5) | |
spinner.color = UIColor.white | |
spinner.hidesWhenStopped = true | |
spinner.startAnimating() | |
self.addSubview(spinner) | |
} | |
func close() { | |
spinner.stopAnimating() | |
spinner.removeFromSuperview() | |
self.removeFromSuperview() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment