pod로 Lottie 설치해야됨...
import UIKit
import Lottie
class LoadingHUD {
private static let sharedInstance = LoadingHUD()
private var backgroundView: UIView?
private var loadingLabel: UILabel?
private var isRun: Bool = false
private var animationView: LottieAnimationView?
class func show() {
let backgroundView = UIView(frame: CGRect.init(x: 0, y: 0, width: 150, height: 150))
backgroundView.tag = 1000
let animation = LottieAnimation.named("loading_loop")
let animationView = LottieAnimationView(animation: animation)
animationView.frame = CGRect(x: 0, y: 0, width: 150, height: 150)
animationView.loopMode = .loop
animationView.contentMode = .scaleAspectFit
animationView.tag = 1001
let loadingLabel = UILabel(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
loadingLabel.text = "Loading ..."
loadingLabel.font = UIFont.boldSystemFont(ofSize: 15)
loadingLabel.textColor = .white
loadingLabel.textAlignment = .center
if let window = UIApplication.shared.keyWindow {
window.addSubview(backgroundView)
window.addSubview(animationView)
window.addSubview(loadingLabel)
window.bringSubviewToFront(backgroundView)
window.bringSubviewToFront(popupView)
backgroundView.frame = CGRect(x: 0, y: 0, width: window.frame.maxX, height: window.frame.maxY)
backgroundView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
animationView.center = window.center
animationView.play()
loadingLabel.layer.position = CGPoint(x: window.frame.midX, y: popupView.frame.maxY+10)
sharedInstance.backgroundView?.removeFromSuperview()
sharedInstance.animationView?.removeFromSuperview()
sharedInstance.loadingLabel?.removeFromSuperview()
sharedInstance.backgroundView = backgroundView
sharedInstance.animationView = animationView
sharedInstance.loadingLabel = loadingLabel
sharedInstance.isRun = true
}
}
class func hide() {
// if let window = UIApplication.shared.keyWindow {
// let backView = window.getView(viewTag: 1000) as? UIView
// let animationView = window.getView(viewTag: 1001) as? LottieAnimationView
// animationView?.stop()
// backView?.removeFromSuperview()
// animationView?.removeFromSuperview()
// sharedInstance.isRun = false
// }
// return
if let animationView = sharedInstance.animationView,
let loadingLabel = sharedInstance.loadingLabel,
let backgroundView = sharedInstance.backgroundView {
animationView.stop()
backgroundView.removeFromSuperview()
animationView.removeFromSuperview()
loadingLabel.removeFromSuperview()
}
sharedInstance.isRun = false
}
class func isLoading() -> Bool {
return sharedInstance.isRun
}
}
[사용방법]
import UIKit
class LodingHUDViewController: UIViewController {
var timer: Timer? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func showButton(_ sender: UIButton) {
if let timer = self.timer {
if timer.isValid {
timer.invalidate()
}
}
LoadingHUD.show()
self.timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: false) { (Timer) in
LoadingHUD.hide()
}
}
}
'Swift > 기타' 카테고리의 다른 글
[IOS] Universal Link 연동 하기 (0) | 2024.06.20 |
---|---|
ITMS-91053: Missing API declaration 대응하기 (0) | 2024.04.08 |
Mac OS Host파일 변경 (0) | 2024.01.02 |
XCode 빌드 번호 자동증가 시키기 (0) | 2023.12.22 |
[SWIFT]FaceID, TouchID 설정화면 이동 (1) | 2023.11.28 |