Created
April 24, 2018 11:56
-
-
Save michal-majchrzycki/74e64797e404aee03e37914768098d48 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
final class Spinner { | |
fileprivate static var activityIndicator: UIActivityIndicatorView? | |
fileprivate static var style: UIActivityIndicatorViewStyle = .whiteLarge | |
fileprivate static var baseBackColor = UIColor(white: 0, alpha: 0.6) | |
fileprivate static var baseColor = UIColor.white | |
/** | |
Add spinner to `UIView` | |
- Parameters: | |
- view: The `UIView` being used to add the `UIActivityIndicatorView` onto | |
- style: Style used for the `UIActivityIndicatorView` | |
- backgroundColor: Display color | |
- baseColor: Tint color of the spinner | |
*/ | |
open static func start(from view: UIView, | |
style: UIActivityIndicatorViewStyle = Spinner.style, | |
backgroundColor: UIColor = Spinner.baseBackColor, | |
baseColor: UIColor = Spinner.baseColor) { | |
guard Spinner.activityIndicator == nil else { return } | |
let spinner = UIActivityIndicatorView(activityIndicatorStyle: style) | |
spinner.backgroundColor = backgroundColor | |
spinner.color = baseColor | |
spinner.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(spinner) | |
// Auto-layout constraints | |
addConstraints(to: view, with: spinner) | |
Spinner.activityIndicator = spinner | |
Spinner.activityIndicator?.startAnimating() | |
} | |
/// Stops and removes `UIActivityIndicatorView` | |
open static func stop() { | |
Spinner.activityIndicator?.stopAnimating() | |
Spinner.activityIndicator?.removeFromSuperview() | |
Spinner.activityIndicator = nil | |
} | |
/** | |
Add auto-layout constraints to provided `UIActivityIndicatorView` | |
- Parameters: | |
- view: The view used to provide layout constraints | |
- spinner: The `UIActivityIndicatorView` used to display | |
*/ | |
fileprivate static func addConstraints(to view: UIView, with spinner: UIActivityIndicatorView) { | |
spinner.topAnchor.constraint(equalTo: view.topAnchor).isActive = true | |
spinner.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true | |
spinner.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true | |
spinner.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment