Last active
May 7, 2020 17:19
-
-
Save michal-majchrzycki/9e41a6ab195e931b5e33336dbf433427 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// 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