Created
January 3, 2022 05:14
-
-
Save kyungpyoda/f1f39f23456b6c710ab22d7c1627f1f5 to your computer and use it in GitHub Desktop.
[iOS] Global Loading Indicator
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
// | |
// LoadingIndicator.swift | |
// | |
// Created by 홍경표 on 2021/12/28. | |
// | |
import UIKit | |
public class LoadingIndicator { | |
private static weak var currentIndicator: UIActivityIndicatorView? | |
public class func showLoading() { | |
// keyWindow 객체 획득 | |
guard let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return } | |
let loadingIndicatorView: UIActivityIndicatorView | |
if let existedView = window.subviews.first(where: { $0 is UIActivityIndicatorView }) as? UIActivityIndicatorView { | |
loadingIndicatorView = existedView | |
} else { | |
loadingIndicatorView = UIActivityIndicatorView(style: .large) | |
} | |
loadingIndicatorView.frame = window.frame | |
loadingIndicatorView.color = .white | |
loadingIndicatorView.backgroundColor = .black.withAlphaComponent(0.5) | |
window.addSubview(loadingIndicatorView) | |
loadingIndicatorView.startAnimating() | |
currentIndicator = loadingIndicatorView | |
} | |
public class func hideLoading() { | |
DispatchQueue.main.async { | |
UIView.animate( | |
withDuration: 0.3, | |
delay: 0, | |
options: .curveEaseInOut, | |
animations: { | |
currentIndicator?.alpha = 0 | |
}, | |
completion: { _ in | |
currentIndicator?.removeFromSuperview() | |
} | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment