-
-
Save konnnn/312fb6d73a81524027905e91b21f6e99 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() | |
} | |
} | |
} | |
// To implement Spinner, just go to the View Controller. Then, in place where indicator should start animating | |
// (i.e. viewDidLoad or viewWillAppear) just put Spinner.start(). Or, if you want personalize style, | |
// backColor and/or baseColor: | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
Spinner.start(style: .white, backgroundColor: UIColor.white, color: UIColor.red) | |
} | |
// Then, in a place where processing is done, simply put: | |
Spinner.stop() | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment