Skip to content

Instantly share code, notes, and snippets.

@michal-majchrzycki
Last active May 7, 2020 17:19
Show Gist options
  • Save michal-majchrzycki/9e41a6ab195e931b5e33336dbf433427 to your computer and use it in GitHub Desktop.
Save michal-majchrzycki/9e41a6ab195e931b5e33336dbf433427 to your computer and use it in GitHub Desktop.
//
// Spinner.swift
//
// Created by Michał Majchrzycki on 28.03.2018.
// Copyright © 2018 Prograils.com. All rights reserved.
// Check whole tutorial at: https://prograils.com/posts/reusable-activity-indicator-with-swift
import UIKit
open class Spinner {
internal static var spinner: UIActivityIndicatorView?
open static var style: UIActivityIndicatorViewStyle = .whiteLarge
open static var baseBackColor = UIColor.black.withAlphaComponent(0.5)
open static var baseColor = UIColor.red
open static func start(style: UIActivityIndicatorViewStyle = style, backColor: UIColor = baseBackColor, baseColor: UIColor = baseColor) {
NotificationCenter.default.addObserver(self, selector: #selector(update), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
if spinner == nil, let window = UIApplication.shared.keyWindow {
let frame = UIScreen.main.bounds
spinner = UIActivityIndicatorView(frame: frame)
spinner!.backgroundColor = backColor
spinner!.activityIndicatorViewStyle = style
spinner?.color = baseColor
window.addSubview(spinner!)
spinner!.startAnimating()
}
}
open static func stop() {
if spinner != nil {
spinner!.stopAnimating()
spinner!.removeFromSuperview()
spinner = nil
}
}
@objc open static func update() {
if spinner != nil {
stop()
start()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment